#include <stdlib.h>
#include <sys/syscall.h>

#include "cms_msg.h"
#include "cms_common.h"
#include "cms_log.h"

#define gettid() syscall(SYS_gettid)	//get thread id for seed

char cmsModuleFile[sizeof(CMS_MODULE_FILE_PATH) + MODULE_NAME_SIZE] = CMS_MODULE_FILE_PATH;
int cmsModuleKey = -1;
int cmsEventKey = -1;
int cmsStatsKey = -1;
int cmsModuleId = -1;
int cmsEventId = -1;
int cmsStatsId = -1;
int cmsSessionId = 1;
pthread_t cmsThreadHandle;
pthread_attr_t threadAttr;


static cmsMsgCb_t *cms_msgShmAppend(cmsMsgCb_t *pMsg, int type, int shmSize)
{
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *newMsg = NULL, *pCurMsg = NULL;
	key_t key = 0;
	int proj_id = 2;
	
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "parameter is NULL.\n");
		return NULL;
	}
	
	/*create new msg*/
	if((newMsg = (cmsMsgCb_t *)calloc(1, sizeof(cmsMsgCb_t))) == NULL){
		CMS_LOG(LL_ERROR, "malloc new msg fail.\n");
		return NULL;
	}
	
	/*create new share mem*/
	newMsg->shmId = -1;
	do{
		key = ftok(cmsModuleFile,proj_id);
		if(key == -1){
			CMS_LOG(LL_ERROR,"ftok key fail, errorCode %d(%s), proj_id %d, pathName %s, shmSize %u, key %d.\n", 
				errno, strerror(errno), 
				proj_id, cmsModuleFile, 
				shmSize, key);
			cms_msgFree(newMsg);
			return NULL;
		}

		newMsg->shmId = shmget(key, shmSize, IPC_CREAT|IPC_EXCL|0666);
		if((newMsg->shmId == -1) && (proj_id++ > 0xFF)){
			CMS_LOG(LL_ERROR,"malloc shm fail, errorCode %d(%s), proj_id %d, pathName %s, shmSize %u, key %u, shmId %d.\n", 
				errno, strerror(errno), 
				proj_id, cmsModuleFile, 
				shmSize, key, newMsg->shmId);
			cms_msgFree(newMsg);
			return NULL;
		}
	}while(newMsg->shmId == -1);
	
	if(proj_id == 0){
		CMS_LOG(LL_ERROR, "shmget fail, error is %s.\n", strerror(errno));
		safe_free(newMsg);
		return NULL;
	}
	
	/*init new msg*/
	newMsg->shmSize = shmSize;
	newMsg->shmAddr = (void *)shmat(newMsg->shmId, 0, 0);
	newMsg->msgOffset = MSG_OFFSET;
	newMsg->length = 0;
	newMsg->pCurData = (uint8_t *)(newMsg->shmAddr) + newMsg->msgOffset;
	newMsg->next = NULL;
	if(type == 0){
		newMsg->msgType |= CMS_MSG_SEND;
		pMsg->pCurMsgCb = newMsg;
	}else{
		newMsg->msgType |= CMS_MSG_REPLY;
	}

	/*add to msg list tail*/
	pCurMsg = pMsg;
	while(pCurMsg->next != NULL){
		pCurMsg = pCurMsg->next;
	}
	
	pCurMsg->next = newMsg;
	memset(newMsg->shmAddr, 0, shmSize);
	pMsg->shmCnt++;

	CMS_LOG(LL_DEBUG, "Exit\n");
	return newMsg;
}

int cms_msgFree(cmsMsgCb_t *pMsg){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL, *tmpMsg = NULL;
	
	if(pMsg == NULL)
		return 0;

	pCurMsg = pMsg;
	/*free cb share memory*/
	if(pCurMsg->msgType & CMS_MSG_RECV){
		while(pCurMsg != NULL){
			if(pCurMsg->shmAddr){
				if(shmdt(pCurMsg->shmAddr) == -1){
					CMS_LOG(LL_ERROR, "shm %d detach fail.\n", pCurMsg->shmId);
				}else{
					CMS_LOG(LL_INFO, "shm %d detach success.\n", pCurMsg->shmId);
				}
			}
			if(pCurMsg->shmId){
				if(shmctl(pCurMsg->shmId, IPC_RMID, NULL) == -1){
					CMS_LOG(LL_ERROR, "shm %d remove fail.\n", pCurMsg->shmId);
				}else{
					CMS_LOG(LL_INFO, "shm %d remove success.\n", pCurMsg->shmId);
				}
			}
			
			pCurMsg = pCurMsg->next;
		}
	}

	/*free cb*/
	pCurMsg = pMsg;
	while(pCurMsg != NULL){
		tmpMsg = pCurMsg;
		pCurMsg = pCurMsg->next;
		
		safe_free( tmpMsg);
	}

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_msgAndShmFree(cmsMsgCb_t *pMsg)
{
	cmsMsgCb_t *pCurMsg = pMsg;
	if(pCurMsg == NULL)
		return CMS_NOK;
	
	while(pCurMsg){
		pCurMsg->msgType &=  ~CMS_MSG_SEND;
		pCurMsg->msgType |=  CMS_MSG_RECV;
		pCurMsg = pCurMsg->next;
	}

	return cms_msgFree(pMsg);
}


/*
	Description:
		The API is for action of select an object by name.
	parameters:
		pMsg - the pointer of request msg 
		objName - the object name, full path
		shmId - the share memory id of xml
	return:
		CMS_OK -  ok
		other error value - fail
*/
int cms_requestSelObjByName(cmsMsgCb_t *pMsg, char *objName){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint8_t sizeOfObjName = 0;
	uint32_t lackSize = 0;
	

	/*parameter check*/
	if((pMsg == NULL) || (objName == NULL)){
		CMS_LOG(LL_ERROR, "parameter is NULL.\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint32_t) + sizeof(uint8_t) + strlen(objName) + 1 + MSG_RESERVED_INDEX;
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_SEL);
	pMsg->header.actionNum++;
	pMsg->header.len += total;

	/*move the the last element*/
	pCurMsg = pMsg;
	while(pCurMsg->next != NULL){
		pCurMsg = pCurMsg->next;
	}

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	//*((uint32_t *)pCurMsg->pCurData) = shmId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	sizeOfObjName = strlen(objName) + 1;
	*((uint8_t *)pCurMsg->pCurData) = sizeOfObjName + MSG_RESERVED_INDEX;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	memcpy(pCurMsg->pCurData, objName, sizeOfObjName);	
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeOfObjName + MSG_RESERVED_INDEX;

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestSelObjById(cmsMsgCb_t *pMsg, uint32_t objId, uint64_t objIA){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint32_t lackSize = 0;
	

	/*parameter check*/
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "parameter is NULL.\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint32_t)*2 + sizeof(uint64_t);
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_SELID);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set act type*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	*((uint32_t *)pCurMsg->pCurData) = objId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	//*((uint32_t *)pCurMsg->pCurData) = shmId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*reserver for IA*/
	*((uint64_t *)pCurMsg->pCurData) = objIA;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint64_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

/*
	Description:
		set request to select object by it's offset.
	parameters:
		pMsg - the pointer of request msg 
		objOffset -  object offset which need select
	return:
		fail -  -1
		success - 0
*/
int cms_requestSelObjByOffset(cmsMsgCb_t *pMsg, cmsoffset_t objOffset){
	CMS_LOG(LL_DEBUG, "Extrance\n");

	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint32_t lackSize = 0;
	
	/*parameter check*/
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "parameter is NULL.\n");
		return -1;
	}
	
	total = sizeof(uint32_t) + sizeof(uint32_t)*2 + sizeof(uint64_t);
	total = (total + 0x3) & ~0x3;
	
	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_SELBYOFFSET);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set type/oid/ia*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint32_t *)pCurMsg->pCurData) = objOffset;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint64_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}


/*
	Description:
		The API is for action of set a rule under indexed object which is selected
		by action selByName or selByOffser.
	parameters:
		pMsg - the pointer of request msg 
		activate - the indicator of add(1) or del(0) a rule of indexed node.
	return:
		CMS_OK -  ok
		other error value - fail
*/
int cms_requestAddObjectByOID(cmsMsgCb_t *pMsg, uint32_t OID, uint64_t objIA){
	CMS_LOG(LL_DEBUG, "Extrance\n");

	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint32_t lackSize = 0;
	
	/*parameter check*/
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "parameter is NULL.\n");
		return -1;
	}
	
	total = sizeof(actionType_t) + sizeof(uint32_t)*2 + sizeof(uint64_t);
	
	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_ADDOBJBYID);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set type/oid/ia*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	*((uint32_t *)pCurMsg->pCurData) = OID;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	//*((uint32_t *)pCurMsg->pCurData) = shmId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*reserver for IA*/
	*((uint64_t *)pCurMsg->pCurData) = objIA;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint64_t);
	

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}


/*
	Description:
		The API is for action of set a rule under indexed object which is selected
		by action selByName or selByOffser.
	parameters:
		pMsg - the pointer of request msg 
		objectPath - object full path
	return:
		CMS_OK -  ok
		other error value - fail
*/
int cms_requestAddObjectByPath(cmsMsgCb_t *pMsg, char *objPathName){
	CMS_LOG(LL_DEBUG, "Extrance\n");

	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t sizeOfobjpath = 0;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint32_t lackSize = 0;
	
	/*parameter check*/
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "parameter is NULL.\n");
		return -1;
	}

	sizeOfobjpath = strlen(objPathName) + 1;
	
	total = sizeof(actionType_t) + sizeof(uint32_t) + sizeof(uint8_t) + sizeOfobjpath + MSG_RESERVED_INDEX;
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_ADDOBJBYPATH);
	pMsg->header.actionNum++;
	pMsg->header.len += total;

	/*move the the last element*/
	pCurMsg = pMsg;
	while(pCurMsg->next != NULL){
		pCurMsg = pCurMsg->next;
	}

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	//*((uint32_t *)pCurMsg->pCurData) = shmId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint8_t *)pCurMsg->pCurData) = sizeOfobjpath + MSG_RESERVED_INDEX;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	memcpy(pCurMsg->pCurData, objPathName, sizeOfobjpath);	
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeOfobjpath + MSG_RESERVED_INDEX;

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestDelObjectByOID(cmsMsgCb_t *pMsg, uint32_t OID, uint64_t objIA){
	CMS_LOG(LL_DEBUG, "Extrance\n");

	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint32_t lackSize = 0;
	
	/*parameter check*/
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "parameter is NULL.\n");
		return -1;
	}
	
	total = sizeof(actionType_t) + sizeof(uint32_t)*2 + sizeof(uint64_t);
	
	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_DELOBJBYID);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set type/oid/ia*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	*((uint32_t *)pCurMsg->pCurData) = OID;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	//*((uint32_t *)pCurMsg->pCurData) = shmId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*reserver for IA*/
	*((uint64_t *)pCurMsg->pCurData) = objIA;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint64_t);
	
	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestDelObjectByPath(cmsMsgCb_t *pMsg, char *objPathName){
	CMS_LOG(LL_DEBUG, "Extrance\n");

	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t sizeOfobjpath = 0;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint32_t lackSize = 0;
	
	/*parameter check*/
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "parameter is NULL.\n");
		return -1;
	}

	sizeOfobjpath = strlen(objPathName) + 1;
	
	total = sizeof(actionType_t) + sizeof(uint32_t) + sizeof(uint8_t) + sizeOfobjpath + MSG_RESERVED_INDEX;
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_DELOBJBYPATH);
	pMsg->header.actionNum++;
	pMsg->header.len += total;

	/*move the the last element*/
	pCurMsg = pMsg;
	while(pCurMsg->next != NULL){
		pCurMsg = pCurMsg->next;
	}

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	//*((uint32_t *)pCurMsg->pCurData) = shmId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);


	*((uint8_t *)pCurMsg->pCurData) = sizeOfobjpath + MSG_RESERVED_INDEX;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	memcpy(pCurMsg->pCurData, objPathName, sizeOfobjpath);	
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeOfobjpath + MSG_RESERVED_INDEX;

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestSetPara(cmsMsgCb_t *pMsg, char *parameter, uint32_t typeSizeOfValue, void *value){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint32_t paraValueSize = 0;
	uint8_t len;
	uint32_t lackSize = 0;

	if((pMsg == NULL) || (parameter == NULL)){
		CMS_LOG(LL_ERROR, "para is NULL.\n");
		return -1;
	}

	/*compute parameter value size*/
	if(typeSizeOfValue == PARAMETER_TYPE_STRING)
		paraValueSize = strlen(value) + 1;
	else
		paraValueSize = typeSizeOfValue & ~((uint32_t)PARAMETER_TYPE_CLASS_BITS);

	/*compute total size, include type/parameterName/parameter value type/value*/
	total = sizeof(actionType_t) + sizeof(uint8_t) + strlen(parameter) + 1;
	total = (total + 0x3) & ~0x3;
	total = total + sizeof(uint32_t) + paraValueSize;
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_SETPARA);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;
	
	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*put info in share memory*/
	/*set parameter*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	len = strlen(parameter) + 1;
	*((uint8_t *)pCurMsg->pCurData) = len;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	memcpy(pCurMsg->pCurData, parameter, len);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + len;

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	/*set parameter value*/
	*((uint32_t *)pCurMsg->pCurData) = typeSizeOfValue;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);
	
	memcpy(pCurMsg->pCurData, value, paraValueSize);

	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + paraValueSize;
	
	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestGetPara(cmsMsgCb_t *pMsg, char *parameter){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint8_t len = 0;
	uint32_t lackSize = 0;

	if((pMsg == NULL) || (parameter == NULL)){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	/*size, reserver 4byte*3 to store parameter offset/attr offset/typeSize*/
	total = sizeof(actionType_t) + sizeof(uint8_t) + strlen(parameter) + 1;
	total = (total + 0x3) & ~0x3;
	total += sizeof(uint32_t)*3;
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_GETPARA);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set para*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	len = strlen(parameter) + 1;
	*((uint8_t *)pCurMsg->pCurData) = len;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	memcpy(pCurMsg->pCurData, parameter, len);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + len;

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	/*reserve size for parameterName/value/typeSize*/
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t)*3;

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestSetAttr(cmsMsgCb_t *pMsg, char *parameter, uint32_t attr, uint32_t active){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0, total = 0;
	uint8_t len;
	uint32_t lackSize = 0;

	if((pMsg == NULL) || (parameter == NULL)){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint32_t)*2 + sizeof(uint8_t) + strlen(parameter) + 1;
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_SETATTR);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	*((uint32_t *)pCurMsg->pCurData) = active;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint32_t *)pCurMsg->pCurData) = attr;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	len = strlen(parameter) + 1;
	*((uint8_t *)pCurMsg->pCurData) = len;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);
		
	memcpy(pCurMsg->pCurData, parameter, len);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + len;

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestGetAttr(cmsMsgCb_t *pMsg, char *parameter){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint8_t paraLen = 0;
	uint32_t lackSize = 0;


	if((pMsg == NULL) || (parameter == NULL)){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	/*size*/
	total = sizeof(actionType_t) + sizeof(uint32_t)*2 + sizeof(uint8_t) + strlen(parameter) + 1;
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_GETATTR);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/* set value type */
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);
	/* reserve for active */
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);
	/* reserve for attr */
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);
	/* set parameter name length */
	paraLen = strlen(parameter) + 1;
	*((uint8_t *)pCurMsg->pCurData) = paraLen;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);
	/* set parameter name*/
	memcpy(pCurMsg->pCurData, parameter, paraLen);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + paraLen;

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestGetObject(cmsMsgCb_t *pMsg, uint32_t objId, uint64_t objIA){
	CMS_LOG(LL_DEBUG, "Extrance\n");

	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint32_t lackSize = 0;
	
	/*parameter check*/
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "parameter is NULL.\n");
		return -1;
	}
	
	total = sizeof(actionType_t) + sizeof(uint32_t)*2 + sizeof(uint64_t);
	total = (total + 0x3) & ~0x3;
	
	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_GETOBJ);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set actType*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	/*set objId & shmId*/
	*((uint32_t *)pCurMsg->pCurData) = objId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	//*((uint32_t *)pCurMsg->pCurData) = shmId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint64_t *)pCurMsg->pCurData) = objIA;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint64_t);
	
	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestSubscribeEvent(cmsMsgCb_t *pMsg, uint32_t numOfEvent, const uint32_t *eventId){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	int total = 0;
	int i;
	uint32_t lackSize = 0;

	SET_TYPE(actionType, CMS_MSG_ACT_SUBSCRIBEEVENT);

	total = sizeof(actionType_t) + sizeof(uint32_t)*(2 + numOfEvent);
	total = (total + 0x3) & ~0x3;
	
	pMsg->header.actionNum++;
	pMsg->header.len += total;//len of header is the size of action struction
	pCurMsg = pMsg->pCurMsgCb;
	
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*add event in shm*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint32_t *)pCurMsg->pCurData) = numOfEvent;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	for(i = 0; i < numOfEvent; i++){
		*((uint32_t *)pCurMsg->pCurData) = *(eventId + i);
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);
	}
	//padding
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestUnsubscribeEvent(cmsMsgCb_t *pMsg, uint32_t numOfEvent, const uint32_t *eventId){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	int total = 0;
	int i;
	uint32_t lackSize = 0;

	SET_TYPE(actionType, CMS_MSG_ACT_UNSUBSCRIBEEVENT);

	total = sizeof(actionType_t) + sizeof(uint32_t)*(2 + numOfEvent);
	total = (total + 0x3) & ~0x3;
	
	pMsg->header.actionNum++;
	pMsg->header.len += total;//len of header is the size of action struction
	pCurMsg = pMsg->pCurMsgCb;
	
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*add event in shm*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint32_t *)pCurMsg->pCurData) = numOfEvent;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	for(i = 0; i < numOfEvent; i++){
		*((uint32_t *)pCurMsg->pCurData) = *(eventId + i);
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);
	}
	//padding
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

static uint32_t getRealParameterTypeSize(uint32_t typeSizeOfValue)
{
	uint32_t realTypeSize = PARAMETER_TYPE_STRING;

	if((typeSizeOfValue & PARAMETER_TYPE_STRING) == PARAMETER_TYPE_STRING){
		realTypeSize = PARAMETER_TYPE_STRING;
	}else if((typeSizeOfValue & PARAMETER_TYPE_SINT63) == PARAMETER_TYPE_SINT63){
		realTypeSize = PARAMETER_TYPE_SINT63;
	}else if((typeSizeOfValue & PARAMETER_TYPE_SINT63) == PARAMETER_TYPE_SINT63){
		realTypeSize = PARAMETER_TYPE_SINT63;
	}else if((typeSizeOfValue & PARAMETER_TYPE_SINT31) == PARAMETER_TYPE_SINT31){
		realTypeSize = PARAMETER_TYPE_SINT31;
	}else if((typeSizeOfValue & PARAMETER_TYPE_SINT15) == PARAMETER_TYPE_SINT15){
		realTypeSize = PARAMETER_TYPE_SINT15;
	}else if((typeSizeOfValue & PARAMETER_TYPE_SINT7) == PARAMETER_TYPE_SINT7){
		realTypeSize = PARAMETER_TYPE_SINT7;
	}else if((typeSizeOfValue & PARAMETER_TYPE_IPV4) == PARAMETER_TYPE_IPV4){
		realTypeSize = PARAMETER_TYPE_IPV4;
	}else if((typeSizeOfValue & PARAMETER_TYPE_BOOLEAN) == PARAMETER_TYPE_BOOLEAN){
		realTypeSize = PARAMETER_TYPE_BOOLEAN;
	}else if((typeSizeOfValue & PARAMETER_TYPE_EXTENSION) == PARAMETER_TYPE_EXTENSION){
		realTypeSize = PARAMETER_TYPE_EXTENSION;
	}else if((typeSizeOfValue & PARAMETER_RESTORE) == PARAMETER_RESTORE){
		realTypeSize = PARAMETER_RESTORE;
	}else if((typeSizeOfValue & PARAMETER_TYPE_DATETIME) == PARAMETER_TYPE_DATETIME){
		realTypeSize = PARAMETER_TYPE_DATETIME;
	}else if((typeSizeOfValue & PARAMETER_TYPE_IPV6) == PARAMETER_TYPE_IPV6){
		realTypeSize = PARAMETER_TYPE_IPV6;
	}else if((typeSizeOfValue & PARAMETER_TYPE_UINT64) == PARAMETER_TYPE_UINT64){
		realTypeSize = PARAMETER_TYPE_UINT64;
	}else if((typeSizeOfValue & PARAMETER_TYPE_MAC) == PARAMETER_TYPE_MAC){
		realTypeSize = PARAMETER_TYPE_MAC;
	}else if((typeSizeOfValue & PARAMETER_TYPE_UINT32) == PARAMETER_TYPE_UINT32){
		realTypeSize = PARAMETER_TYPE_UINT32;
	}else if((typeSizeOfValue & PARAMETER_TYPE_UINT16) == PARAMETER_TYPE_UINT16){
		realTypeSize = PARAMETER_TYPE_UINT16;
	}else if((typeSizeOfValue & PARAMETER_TYPE_UINT8) == PARAMETER_TYPE_UINT8){
		realTypeSize = PARAMETER_TYPE_UINT8;
	}

	return realTypeSize;
}


int cms_requestParaInfo(cmsMsgCb_t *pMsg, char *parameterName, uint32_t typeSizeOfValue, void *value){
	CMS_LOG(LL_DEBUG, "Extrance\n");

	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint32_t len = 0;
	uint32_t valueType = 0;
	uint32_t lackSize = 0;
	
	SET_TYPE(actionType, CMS_MSG_ACT_PARAMETERINFO);
	
	valueType = getRealParameterTypeSize(typeSizeOfValue);
	if((valueType & PARAMETER_TYPE_STRING) == PARAMETER_TYPE_STRING)
		total = sizeof(actionType_t) + sizeof(uint32_t) + strlen(value) + 1;
	else
		total = sizeof(actionType_t) + sizeof(uint32_t) + (valueType&~((uint32_t)PARAMETER_TYPE_CLASS_BITS));
	
	total = (total + 0x3) & ~0x3;

	total += sizeof(uint32_t)*2 + sizeof(uint8_t) + strlen(parameterName) + 1;
	total = (total + 0x3) & ~0x3;
	
	/*increase act num and len, set act type*/
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set actType*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	/*set para value*/
	*((uint32_t *)pCurMsg->pCurData) = valueType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	if(valueType == PARAMETER_TYPE_STRING){
		len = strlen(value) + 1;
	}else{
		len = valueType&~((uint32_t)PARAMETER_TYPE_CLASS_BITS);
	}
	
	memcpy(pCurMsg->pCurData, value, len);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) +len;
	
	/*padding, value_s*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
	
	/*set para attr*/
//	*((uint32_t *)pCurMsg->pCurData) = active;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);
	
//	*((uint32_t *)pCurMsg->pCurData) = attr;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);


	len = strlen(parameterName) + 1;
	*((uint8_t *)pCurMsg->pCurData) = len;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	memcpy(pCurMsg->pCurData, parameterName, len);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) +len;
	
	/*padding, attr_s*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
	
	
	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_setSaveActionRequest(cmsMsgCb_t *pMsg, uint32_t shmId){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0, total = 0;
	uint32_t lackSize = 0;

	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint32_t) + sizeof(uint32_t);
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_SAVE);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	*((uint32_t *)pCurMsg->pCurData) = shmId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint32_t *)pCurMsg->pCurData) = getpid();
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestRestoreDefault(cmsMsgCb_t *pMsg, uint32_t type){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0, total = 0;
	uint32_t lackSize = 0;

	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint32_t)+ sizeof(uint8_t);
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_RESTORDEFAULT);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	/*set restore type*/
	*((uint32_t *)pCurMsg->pCurData) = type;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint8_t *)pCurMsg->pCurData) = 0;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestObjRestore(cmsMsgCb_t *pMsg, cmsoffset_t objOffset, uint32_t restoreType)
{
	CMS_LOG(LL_DEBUG, "Extrance\n");

	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint32_t lackSize = 0;
	
	/*parameter check*/
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "parameter is NULL.\n");
		return -1;
	}
	
	total = sizeof(uint32_t) + sizeof(uint32_t)*2 + sizeof(uint64_t);
	total = (total + 0x3) & ~0x3;
	
	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_OBJECT_RESTORE);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set type/objOffset/restoreType*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint32_t *)pCurMsg->pCurData) = objOffset;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint32_t *)pCurMsg->pCurData) = restoreType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint64_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestRestoreConf(cmsMsgCb_t *pMsg, char *fileName){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0, total = 0;
	uint8_t len = 0;
	uint32_t lackSize = 0;

	if((pMsg == NULL) || (fileName == NULL)){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint8_t) + strlen(fileName) + 1;
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_RESTORECONF);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	len = strlen(fileName) + 1;
	*((uint8_t *)pCurMsg->pCurData) = len;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	memcpy(pCurMsg->pCurData, fileName, len);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) +len;
	
	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestSavePreConf(cmsMsgCb_t *pMsg, char *fileName)
{
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0, total = 0;
	uint8_t len = 0;
	uint32_t lackSize = 0;

	if((pMsg == NULL) || (fileName == NULL)){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint8_t) + strlen(fileName) + 1;
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_PRE_DEFAULT);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	len = strlen(fileName) + 1;
	*((uint8_t *)pCurMsg->pCurData) = len;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	memcpy(pCurMsg->pCurData, fileName, len);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) +len;
	
	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}


int cms_requestSetLog(cmsMsgCb_t *pMsg, uint32_t logLevel){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0, total = 0;
	uint32_t lackSize = 0;

	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint32_t)+ sizeof(uint8_t);
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_SET_LOG);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	/*set logLevel*/
	*((uint32_t *)pCurMsg->pCurData) = logLevel;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint8_t *)pCurMsg->pCurData) = 0;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}


int cms_requestQueueClean(cmsMsgCb_t *pMsg){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0, total = 0;
	uint32_t lackSize = 0;

	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint32_t)+ sizeof(uint8_t);
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_QUEUE_CLEAN);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	*((uint32_t *)pCurMsg->pCurData) = 0;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint8_t *)pCurMsg->pCurData) = 0;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestGetAppEvent(cmsMsgCb_t *pMsg, char *moduleName)
{
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0, total = 0;
	uint8_t len = 0;
	uint32_t lackSize = 0;

	if((pMsg == NULL) || (moduleName == NULL)){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint8_t) + strlen(moduleName) + 1;
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_GET_APP_EVENT);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	len = strlen(moduleName) + 1;
	*((uint8_t *)pCurMsg->pCurData) = len;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	memcpy(pCurMsg->pCurData, moduleName, len);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) +len;
	
	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}


int cms_requestMsg(cmsMsgCb_t *pMsg, int requestType)
{
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0, total = 0;
	uint32_t lackSize = 0;

	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint32_t)+ sizeof(uint8_t);
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, requestType);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	*((uint32_t *)pCurMsg->pCurData) = 0;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint8_t *)pCurMsg->pCurData) = 0;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}


int cms_requestSetModuleId(cmsMsgCb_t *pMsg, uint32_t srcModuleId){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0, total = 0;
	uint32_t lackSize = 0;

	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "para is NULL\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint32_t)+ sizeof(uint8_t);
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_SETMODULEID);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	/*set srcModuleId*/
	*((uint32_t *)pCurMsg->pCurData) = srcModuleId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint8_t *)pCurMsg->pCurData) = 0;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}



uint32_t genKeyByName(const char *moduleName, int *cmsModuleKey, int *cmsEventKey){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	char mName[64] = {0};
	int pid = 0;
	
	if(!strcmp(moduleName, CMS_DB)){
		if(cmsModuleKey != NULL)
			*cmsModuleKey = 0x100100;
		if(cmsEventKey != NULL)
			*cmsEventKey = 0x100101;
	}
	else if(!strcmp(moduleName, CMS_CORE)){
		if(cmsModuleKey != NULL)
			*cmsModuleKey = 0x100200;
		if(cmsEventKey != NULL)
			*cmsEventKey = 0x100201;
	}
	else if(!strcmp(moduleName, CMS_TASK)){
		if(cmsModuleKey != NULL)
			*cmsModuleKey = 0x100300;
		if(cmsEventKey != NULL)
			*cmsEventKey = 0x100301;
	}
	else if(!strcmp(moduleName, CMS_SAMPLE)){
		if(cmsModuleKey != NULL)
			*cmsModuleKey = 0x100400;
		if(cmsEventKey != NULL)
			*cmsEventKey = 0x100401;
	}
	else if(!strcmp(moduleName, CMS_EVENT)){
		if(cmsModuleKey != NULL)
			*cmsModuleKey = 0x100500;
		if(cmsEventKey != NULL)
			*cmsEventKey = 0x100501;
	}
	else if(!strcmp(moduleName, CMS_CWMP)){
		if(cmsModuleKey != NULL)
			*cmsModuleKey = 0x100600;
		if(cmsEventKey != NULL)
			*cmsEventKey = 0x100601;
	}
	else if(!strcmp(moduleName, CMS_DBUS)){
		if(cmsModuleKey != NULL)
			*cmsModuleKey = 0x100700;
		if(cmsEventKey != NULL)
			*cmsEventKey = 0x100701;
	}
	else if(!strcmp(moduleName, CMS_LOOPDETECT)){
		if(cmsModuleKey != NULL)
			*cmsModuleKey = 0x100800;
		if(cmsEventKey != NULL)
			*cmsEventKey = 0x100801;
	}
	else if(!strcmp(moduleName, CMS_DNSMASQ)){
		if(cmsModuleKey != NULL)
			*cmsModuleKey = 0x100900;
		if(cmsEventKey != NULL)
			*cmsEventKey = 0x100901;
	}
	else if(strstr(moduleName, ".cgi")){
		if(2 != sscanf(moduleName, "%d_%63s", &pid, mName)) {
            if(cmsEventKey != NULL)
                *cmsEventKey = -1;
            if(cmsModuleKey != NULL)
                *cmsModuleKey = -1;
            CMS_LOG(LL_ERROR, "moduleName[%s] invalid. format: [pid]_[name]. For example: 1234_network.cgi\n", moduleName);
            return -1;
        }

        if(0 == pid) {
            if(cmsEventKey != NULL)
                *cmsEventKey = -1;
            if(cmsModuleKey != NULL)
                *cmsModuleKey = -1;
            CMS_LOG(LL_ERROR, "pid error. moduleName[%s]\n", moduleName);
            return -1;
        }
        
		if(cmsModuleKey != NULL)
			*cmsModuleKey = (CMS_KEY_HTTPD_PREFIX | CMS_KEY_MQ_PREFIX | pid); 
		if(cmsEventKey != NULL)
			*cmsEventKey = (CMS_KEY_HTTPD_PREFIX | CMS_KEY_EQ_PREFIX | pid);
	}
	else{
		pid = getpid();
		pid = (pid & CMS_KEY_PID_MASK);
		if(cmsModuleKey != NULL)
			*cmsModuleKey = (CMS_KEY_OTHERS_PREFIX | CMS_KEY_MQ_PREFIX | pid); 
		if(cmsEventKey != NULL)
			*cmsEventKey = (CMS_KEY_OTHERS_PREFIX | CMS_KEY_EQ_PREFIX | pid);
	}

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int createSessionId(void){
	uint32_t sessionId = 0;

	/*if((sessionId = ftok(cmsModuleFile, cmsSessionId)) == -1)
		CMS_LOG(LL_ERROR, "create session id fail [%s][%d][%s].\n", cmsModuleFile,cmsSessionId,strerror(errno));

	cmsSessionId++;*/

	return sessionId;
}

int cms_requestRegModule(cmsMsgCb_t *pMsg, const char *moduleName, uint32_t pid, uint32_t moduleMsqId, uint32_t eventMsqId){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	
	cmsMsgCb_t  *pCurMsg = NULL;
	uint32_t	actType = 0;
	uint32_t 	total = 0;
	uint8_t		len = 0;
	uint32_t 	lackSize = 0;
	
	SET_TYPE(actType, CMS_MSG_ACT_REGMODULE);
	total = sizeof(actionType_t) + sizeof(uint32_t)*3 + sizeof(uint8_t) + strlen(moduleName) + 1;
	total = (total + 0x3) & ~0x3;

	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set module info in share mem*/
	*((uint32_t *)pCurMsg->pCurData) = actType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	*((uint32_t *)pCurMsg->pCurData) = pid;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint32_t *)pCurMsg->pCurData) = moduleMsqId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	*((uint32_t *)pCurMsg->pCurData) = eventMsqId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	len = strlen(moduleName) + 1;
	*((uint8_t *)pCurMsg->pCurData) = len;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	memcpy(pCurMsg->pCurData, moduleName, len);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + len;

	//padding
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_requestUnRegModule(cmsMsgCb_t *pMsg, const char *moduleName){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t	actType = 0;
	uint32_t 	total = 0;
	uint8_t		len = 0;
	uint32_t 	lackSize = 0;
	pid_t pid;
	
	SET_TYPE(actType, CMS_MSG_ACT_UNREGMODULE);
	total = sizeof(actionType_t) + sizeof(uint32_t)*3 + sizeof(uint8_t) + strlen(moduleName) + 1;
	total = (total + 0x3) & ~0x3;

	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	pid = getpid();
	
	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set module info*/
	*((uint32_t *)pCurMsg->pCurData) = actType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	*((uint32_t *)pCurMsg->pCurData) = pid;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);
	
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t)*2;

	len = strlen(moduleName) + 1;
	*((uint8_t *)pCurMsg->pCurData) = len;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);

	memcpy(pCurMsg->pCurData, moduleName, len);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + len;

	//padding
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}


int cms_registerModule(const char *moduleName){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	
	cmsMsgCb_t *pReq = NULL, *pRecv = NULL;
	int msqMId = 0, msqEId = 0, pid = 0;

	if((moduleName == NULL) || (strlen(moduleName) == 0)){
		CMS_LOG(LL_ERROR,"register module fail, moduleName if NUll.\n");
		return CMS_NOK;
	}

	genKeyByName(moduleName, &cmsModuleKey, &cmsEventKey);
	if((cmsModuleKey <= 0) || (cmsEventKey <= 0)){
		CMS_LOG(LL_ERROR, "register module fail,key value is wrong. cmsModuleKey 0x%x, cmsEventKey 0x%x.",
				cmsModuleKey, cmsEventKey);
		return CMS_NOK;
	}

	if(!strcmp(moduleName, CMS_TASK)){
		cmsStatsKey = CMSTASK_STATS_KEY;

		if((cmsStatsId = msgget(cmsStatsKey, 0666)) == -1){
			do
			{
				cmsStatsId = msgget(cmsStatsKey, 0666 | IPC_CREAT);
				if(cmsStatsId == -1){
					CMS_LOG(LL_ERROR, "msgget task stats queue fail: %s.\n",strerror(errno));
				}else if(cmsStatsId == 0){
					CMS_LOG(LL_DEBUG,"msqEid is 0,remove it.\n");
					if((msgctl(cmsStatsId, IPC_RMID, NULL)) == -1){
						CMS_LOG(LL_ERROR, "remove cmsStatsId fail.\n");
						return CMS_NOK;
					}
				}else{
					CMS_LOG(LL_DEBUG, "create cmsStatsId success.\n");
					break;
				}
			}while(cmsStatsId == -1);
		}
	}
	
	/*get or create message queue*/
	if((msqMId = msgget(cmsModuleKey, 0666)) == -1){
		do
		{
			msqMId = msgget(cmsModuleKey, 0666 | IPC_CREAT);
			if(msqMId == -1){
				CMS_LOG(LL_ERROR, "msgget module queue fail: %s.\n",strerror(errno));
			}else if(msqMId == 0){
				CMS_LOG(LL_DEBUG, "msqMid is 0,remove it.\n");
				if((msgctl(msqMId, IPC_RMID, NULL)) == -1){
					CMS_LOG(LL_ERROR, "remove msqMid fail.\n");
					return CMS_NOK;
				}
			}else{
				CMS_LOG(LL_DEBUG, "create msqMId success.\n");
				break;
			}
		}while(msqMId == -1);
	}

	/*get or create event queue*/
	if((msqEId = msgget(cmsEventKey, 0666)) == -1){
		do
		{
			msqEId = msgget(cmsEventKey, 0666 | IPC_CREAT);
			if(msqEId == -1){
				CMS_LOG(LL_ERROR, "msgget event queue fail: %s.\n",strerror(errno));
			}else if(msqEId == 0){
				CMS_LOG(LL_DEBUG,"msqEid is 0,remove it.\n");
				if((msgctl(msqEId, IPC_RMID, NULL)) == -1){
					CMS_LOG(LL_ERROR, "remove msqEId fail.\n");
					goto error1;
				}
			}else{
				CMS_LOG(LL_DEBUG, "create msqEid success.\n");
				break;
			}
		}while(msqEId == -1);
	}

	cmsModuleId = msqMId;
	cmsEventId = msqEId;
	pid = getpid();

	char cmdStr[64] = {0};
	sprintf(cmdStr, "touch /tmp/%s_key", moduleName);
	cms_do_cmd(cmdStr);
	memset(cmdStr, 0, sizeof(cmdStr));
	sprintf(cmdStr, "/tmp/%s_key", moduleName);
	if(access(cmdStr, F_OK) != -1){
		memset(cmsModuleFile, 0, sizeof(cmsModuleFile));
		sprintf(cmsModuleFile, "/tmp/%s_key", moduleName);
	}

	if((pReq = cms_createRequestMsg(0)) == NULL){
		goto error2;
	}

	CMS_LOG(LL_DEBUG, "register module, name %s, mid %d, eid %d.\n", moduleName, msqMId, msqEId);
	cms_requestRegModule(pReq,moduleName,pid,msqMId,msqEId);
	if((pRecv = cms_SendRecvMsg(pReq, 0, 60000)) == NULL){
		CMS_LOG(LL_ERROR,"cms send/recv msg fail.\n");
		goto error3; 
	}
	cms_msgFree(pReq);	

	CMS_LOG(LL_DEBUG, "==================recv msg header flag is 0x%x==============================\n",pRecv->header.flag);
	if(IS_NACK(pRecv->header.flag)){
		CMS_LOG(LL_ERROR,"register module fail, return nack.\n");
		goto error4;
	}

	cms_msgFree(pRecv);
	
	CMS_LOG(LL_INFO,"************************regiser %s module sucess***************************\n", moduleName);
	CMS_LOG(LL_INFO,"************************module key: 0x%x, id: %u**************************\n", cmsModuleKey, cmsModuleId);
	CMS_LOG(LL_INFO,"************************event key: 0x%x, id: %u***************************\n", cmsEventKey, cmsEventId);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return cmsModuleId;
	
error4:
	if(pRecv)
		cms_msgFree(pRecv);	
error3:
	if(pReq)
		cms_msgFree(pReq);
error2:
	if(msqEId > 0)
		msgctl(msqEId, IPC_RMID, NULL);
error1:
	if(msqMId > 0)
		msgctl(msqMId, IPC_RMID, NULL);

	CMS_LOG(LL_ERROR, "Exit, fail\n");
	return CMS_NOK;
}

int cms_unregisterModule(const char *moduleName){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	
	cmsMsgCb_t *pReq = NULL, *pRecv = NULL;

	if(moduleName == NULL){
		CMS_LOG(LL_ERROR,"unregister module fail, moduleName is NULL.\n");
		return -1;
	}

	if((pReq = cms_createRequestMsg(0)) == NULL){
		CMS_LOG(LL_ERROR,"create request msg fail.\n");
		return -1;
	}

	cms_requestUnRegModule(pReq, moduleName);
	if((pRecv = cms_SendRecvMsg(pReq, 0, 500)) == NULL){
		CMS_LOG(LL_ERROR,"send_recv request msg fail.\n");
	}
	cms_msgFree(pReq);

	if(pRecv){
		if(IS_NACK(pRecv->header.flag)){
			CMS_LOG(LL_ERROR,"recv msg is nack.\n");
		}
		cms_msgFree(pRecv);
	}

	if((msgctl(cmsModuleId, IPC_RMID, NULL)) == -1){
		CMS_LOG(LL_ERROR,"rm module msg fail.\n");
		return -1;
	}
	if((msgctl(cmsEventId, IPC_RMID, NULL)) == -1){
		CMS_LOG(LL_ERROR,"rm event msg fail.\n");
		return -1;
	}

	char cmdStr[64] = {0};
	sprintf(cmdStr, "/tmp/%s_key", moduleName);
	if(access(cmdStr, F_OK) != -1){
		memset(cmdStr, 0, sizeof(cmdStr));
		sprintf(cmdStr, "rm -rf /tmp/%s_key", moduleName);
		cms_do_cmd(cmdStr);

		memset(cmsModuleFile, 0, sizeof(cmsModuleFile));
		strcpy(cmsModuleFile, CMS_MODULE_FILE_PATH);
	}
	
	CMS_LOG(LL_INFO,"************************unregiser %s module sucess***************************\n", moduleName);
	CMS_LOG(LL_INFO,"************************module key: 0x%x, id: %u**************************\n", cmsModuleKey, cmsModuleId);
	CMS_LOG(LL_INFO,"************************event key: 0x%x, id: %u***************************\n", cmsEventKey, cmsEventId);
	CMS_LOG(LL_INFO,"**************************************************************************\n");

	//cmsEventId = -1;
	//cmsModuleId = -1;
	
	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_getModuleIdByName(const char *moduleName){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	
	int moduleId = -1;
	int cmsModuleKey = -1;

	if((moduleName == NULL) || (strlen(moduleName) == 0)){
		CMS_LOG(LL_ERROR, "get module fail, moduleName is NUll.\n");
		return -1;
	}

	genKeyByName(moduleName, &cmsModuleKey, NULL);
	moduleId = msgget(cmsModuleKey, 0666);
	
	CMS_LOG(LL_DEBUG, "Exit\n");
	
	return moduleId;
}

int cms_requestGetModuleId(cmsMsgCb_t *pMsg, char *moduleName){
	CMS_LOG(LL_DEBUG, "Extrance\n");

	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0, total = 0;
	uint8_t len = 0;
	uint32_t lackSize = 0;
	
	if((moduleName == NULL) || (pMsg == NULL)){
		CMS_LOG(LL_ERROR, "get module id fail, parameter if NUll.\n");
		return -1;
	}
	
	total = sizeof(actionType_t) + sizeof(uint32_t)*3 + sizeof(uint8_t) + ((strlen(moduleName) + 1));
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_GETMODULEID);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	/*reserve for moduleMsqId, eventMsqId, pid*/
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t)*3;
	
	len = strlen(moduleName) + 1;
	*((uint8_t *)pCurMsg->pCurData) = len;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint8_t);
	
	memcpy(pCurMsg->pCurData, moduleName, len);
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + len;

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

#if 0
int cms_requestEventNotify(cmsMsgCb_t *pMsg, uint32_t eventId, uint32_t typeSizeOfContent, void *eventContent){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	int total = 0;
	int contentSize = 0;
	uint32_t lackSize = 0;
	

	pMsg->header.flag = CMS_MSG_EVENT;
	SET_TYPE(actionType, CMS_MSG_ACT_EVENT_NOTIFY);

	if(typeSizeOfContent == PARAMETER_TYPE_STRING){
		if(eventContent != NULL)
			contentSize = strlen(eventContent) + 1;
	}else
		contentSize = typeSizeOfContent;


	total = sizeof(actionType_t) + sizeof(uint32_t)*3;
	total = (total + 0x3) & ~0x3;
	total += sizeof(uint32_t) + contentSize;
	total = (total + 0x3) & ~0x3;
	
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0);
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	/*reserved space for ramId*/
	*((uint32_t *)pCurMsg->pCurData) = 0;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*append event count*/
	*((uint32_t *)pCurMsg->pCurData) = 1;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*append event id*/
	*((uint32_t *)pCurMsg->pCurData) = eventId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	/*append typesizeOfContent*/
	*((uint32_t *)pCurMsg->pCurData) = typeSizeOfContent;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*append content*/
	if(eventContent != NULL){
		memcpy(pCurMsg->pCurData, eventContent, contentSize);
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + contentSize;
	}

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

#else
int cms_requestEventNotify(cmsMsgCb_t *pMsg, uint32_t eventId, uint32_t typeSizeOfContent, void *eventContent){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	int total = 0;
	int contentSize = 0;
	uint32_t lackSize = 0;
	

	pMsg->header.flag = CMS_MSG_EVENT;
	SET_TYPE(actionType, CMS_MSG_ACT_EVENT_NOTIFY);

	if(typeSizeOfContent == PARAMETER_TYPE_STRING){
		if(eventContent != NULL)
			contentSize = strlen(eventContent) + 1;
		else 
			contentSize = 1;
	}else{
		contentSize = typeSizeOfContent & ~((uint32_t)PARAMETER_TYPE_CLASS_BITS);
	}


	total = sizeof(actionType_t) + sizeof(uint32_t)*3;
	total = (total + 0x3) & ~0x3;
	total += sizeof(uint32_t) + contentSize;
	total = (total + 0x3) & ~0x3;
	
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	/*reserved space for ramId*/
	*((uint32_t *)pCurMsg->pCurData) = 0;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*append event count*/
	*((uint32_t *)pCurMsg->pCurData) = 1;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*append event id*/
	*((uint32_t *)pCurMsg->pCurData) = eventId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	/*append typesizeOfContent*/
	*((uint32_t *)pCurMsg->pCurData) = typeSizeOfContent;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*append content*/
	if(eventContent != NULL){
		memcpy(pCurMsg->pCurData, eventContent, contentSize);
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + contentSize;
	}else{
		memcpy(pCurMsg->pCurData, "\0", 1);
		if(typeSizeOfContent == PARAMETER_TYPE_STRING){
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + 1;
		}else{
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + contentSize;
		}
	}

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}
#endif

int cms_requestObjChgNotify(cmsMsgCb_t *pMsg, cmsMsgCb_t *objDataMsg){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	int total = 0;
	uint32_t lackSize = 0;

	pMsg->header.flag = CMS_MSG_EVENT;
	SET_TYPE(actionType, CMS_MSG_ACT_OBJCHG_NOTIFY);

	total = sizeof(actionType_t) + sizeof(uint32_t);
	total = (total + 0x3) & ~0x3;


	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	/*shmid for objchg data*/
	*((uint32_t *)pCurMsg->pCurData) = objDataMsg->shmId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);
	
	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	objDataMsg->pCurData = (uint8_t *)(objDataMsg->shmAddr) + objDataMsg->msgOffset;
	*((cmsMsg_header_t*)objDataMsg->pCurData) = objDataMsg->header;
	
	if((shmdt(objDataMsg->shmAddr)) == -1){
		CMS_LOG(LL_ERROR, "detach objdata share memory fail.\n");
	}else{
		CMS_LOG(LL_DEBUG, "detach objdata share memory success.\n");
	}

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;

}

int cms_requestPutPara(cmsMsgCb_t *pMsg, cmsMsgCb_t *objParaMsg){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	int total = 0;
	uint32_t lackSize = 0;

	/*set action type*/
	SET_TYPE(actionType, CMS_MSG_ACT_PUT_PARAINFO);

	/*compute size*/
	total = sizeof(actionType_t) + sizeof(uint32_t);
	total = (total + 0x3) & ~0x3;

	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	/*parameter space for shmId*/
	*((uint32_t *)pCurMsg->pCurData) = objParaMsg->shmId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);
	printf("1shmid of para is %d============================\n", objParaMsg->shmId);
	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	/*point to the header*/
	objParaMsg->pCurData = (uint8_t *)(objParaMsg->shmAddr) + objParaMsg->msgOffset;
		*((cmsMsg_header_t*)objParaMsg->pCurData) = objParaMsg->header;

	/*deatch*/	
	if((shmdt(objParaMsg->shmAddr)) == -1){
		CMS_LOG(LL_ERROR, "detach objdata share memory fail.\n");
	}else{
		CMS_LOG(LL_DEBUG, "detach objdata share memory success.\n");
	}

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;

}


int cms_requestObjChange(cmsMsgCb_t *pMsg, uint32_t realID, uint32_t objId, uint64_t objIA, uint32_t flag, uint32_t objSize, void *oldObjData, void *newObjData ){
	CMS_LOG(LL_DEBUG, "Extrance");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	int total = 0;

	
	pMsg->header.flag = CMS_MSG_EVENT;
	SET_TYPE(actionType, CMS_MSG_ACT_OBJCHG_DATA);

	if(flag == CMS_DB_SET_OBJECT)
		total = sizeof(actionType_t) + sizeof(objectChgData_t) + (objSize*2);
	else 
		total = sizeof(actionType_t) + sizeof(objectChgData_t) + objSize;

	total = (total + 0x3) & ~0x3;
	
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*set value*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	*((uint32_t *)pCurMsg->pCurData) = realID;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*append obj id*/
	*((uint32_t *)pCurMsg->pCurData) = objId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*append obj IA*/
	*((uint64_t *)pCurMsg->pCurData) = objIA;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint64_t);

	/*obj size*/
	*((uint32_t *)pCurMsg->pCurData) = ((objSize & CMS_DB_OBJ_SIZE_MASK) | (flag & CMS_DB_FLAG_MASK));
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	if((flag == CMS_DB_SET_OBJECT) || (flag == CMS_DB_DEL_OBJECT)){
		CMS_LOG(LL_DEBUG, "obj del \n");
		if(oldObjData != NULL)
			memcpy(pCurMsg->pCurData, oldObjData, objSize);
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + objSize;
	}
	
	if((flag == CMS_DB_SET_OBJECT) || (flag == CMS_DB_ADD_OBJECT)){
		CMS_LOG(LL_DEBUG, "obj added \n");
		if(newObjData != NULL)
			memcpy(pCurMsg->pCurData, newObjData, objSize);
		
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + objSize;
	}
	
	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;

}

int cms_requestUpdate(cmsMsgCb_t *pMsg, uint32_t objId, uint64_t objIA){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	uint32_t actionType = 0;
	uint32_t total = 0;
	uint32_t lackSize = 0;

	/*parameter check*/
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "parameter is NULL.\n");
		return -1;
	}

	total = sizeof(actionType_t) + sizeof(uint32_t)*2 + sizeof(uint64_t);
	total = (total + 0x3) & ~0x3;

	/*increase act num and len, set act type*/
	SET_TYPE(actionType, CMS_MSG_ACT_UPDATE);
	pMsg->header.actionNum++;
	pMsg->header.len += total;
	pCurMsg = pMsg->pCurMsgCb;

	/*alloc new msg when mem is not enough*/
	lackSize = pCurMsg->shmSize - (pCurMsg->pCurData - pCurMsg->shmAddr);
	if(lackSize < total){
		pCurMsg = cms_msgShmAppend(pMsg, 0, ((total > MSG_SHM_SIZE) ? total+MSG_OFFSET : MSG_SHM_SIZE));
		if(pCurMsg == NULL)
			return -1;
	}

	/*set act type*/
	*((uint32_t *)pCurMsg->pCurData) = actionType;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t);

	*((uint32_t *)pCurMsg->pCurData) = objId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	//*((uint32_t *)pCurMsg->pCurData) = shmId;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t);

	/*reserver for IA*/
	*((uint64_t *)pCurMsg->pCurData) = objIA;
	pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint64_t);

	/*padding*/
	pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
	
	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}


//debug ----------------- start, get the pid name --
static char *cmsDebug_getProcessName(void){
	char buf[128];
	char pname[32];
	char *p = NULL;

	memset(buf, 0, sizeof(buf));

	if(readlink("/proc/self/exe", buf, sizeof(buf)-1) <= 0){
		return NULL;
	}

	p = strrchr(buf, '/');
	if(p){
		strncpy(pname, p, sizeof(pname)-1);
		pname[sizeof(pname)-1] = '\0';
		return strdup(pname);
	}
	
	return NULL;
}
//debug -----------------


cmsMsgCb_t* cms_createRequestMsg(uint32_t shmSize){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	
	cmsMsgCb_t *pReqMsg = NULL;
	int proj_id = 1;
	key_t key = 0;

	if(shmSize == 0)
		shmSize = MSG_SHM_SIZE;
	
	if((MSG_SHM_SIZE <= 0) || (MSG_SHM_SIZE > 1024*1024*32)){
		CMS_LOG(LL_ERROR,"shmsize is wronog, %d.\n", MSG_SHM_SIZE);
		return NULL;
	}

	pReqMsg = (cmsMsgCb_t *)calloc(1, sizeof(cmsMsgCb_t));
	if(pReqMsg == NULL){
		CMS_LOG(LL_ERROR,"malloc req msg fail.\n");
		return NULL;
	}

	pReqMsg->shmId = -1;
	do{
		key = ftok(cmsModuleFile,proj_id);
		if(key == -1){
			if(errno == 2){//file not exist, del mistake
				char cmdStr[64] = {0};
				sprintf(cmdStr, "touch %s", cmsModuleFile);
				cms_do_cmd(cmdStr);
				key = ftok(cmsModuleFile,proj_id);
			}
			if(key == -1){
				CMS_LOG(LL_ERROR,"ftok key fail, errorCode %d(%s), proj_id %d, pathName %s, shmSize %u, key %d.\n", 
					errno, strerror(errno), 
					proj_id, cmsModuleFile, 
					shmSize, key);
				cms_msgFree(pReqMsg);
				return NULL;
			}
		}
		
		pReqMsg->shmId = shmget(key, shmSize, IPC_CREAT|IPC_EXCL|0666);
		if((pReqMsg->shmId == -1) && (proj_id++ > 0xFF)){
			CMS_LOG(LL_ERROR,"malloc shm fail, errorCode %d(%s), proj_id %d, pathName %s, shmSize %u, key %u, shmId %d.\n", 
				errno, strerror(errno), 
				proj_id, cmsModuleFile, 
				shmSize, key, pReqMsg->shmId);
			cms_msgFree(pReqMsg);
			return NULL;
		}
	}while(pReqMsg->shmId == -1);
	
	if(proj_id == 0){
		CMS_LOG(LL_ERROR,"shmget fail, error is %s.\n", strerror(errno));
		safe_free(pReqMsg);
		return NULL;
	}

	pReqMsg->shmSize = shmSize;
	pReqMsg->shmAddr = (void *)shmat(pReqMsg->shmId, 0, 0);
	pReqMsg->msgOffset = MSG_OFFSET;
	pReqMsg->msgType |= CMS_MSG_SEND;
	pReqMsg->length = 0;
	pReqMsg->shmCnt = 1;

	char *pidname = cmsDebug_getProcessName();
	if(pidname){
		strcpy(pReqMsg->header.srcPidname, pidname);
		free(pidname);
		pidname = NULL;
	}
	snprintf(pReqMsg->header.srcModuleName, strlen(cmsModuleFile)+1, "%s", cmsModuleFile);
	pReqMsg->header.srcModuleId = cmsModuleId; // module src id is msqid
	pReqMsg->header.dstModuleId = 0;
	pReqMsg->header.sessionId = createSessionId();
	pReqMsg->header.actionNum = 0;
	pReqMsg->header.len = sizeof(cmsMsg_header_t);
	pReqMsg->header.flag |= CMS_MSG_REQUEST;
	pReqMsg->header.taskNotify = TRUE;
	pReqMsg->header.cwmpNotify = TRUE;
	pReqMsg->header.isMutliObj = FALSE;
	pReqMsg->header.msgType = 0 ;
	
	
	pReqMsg->pCurData = (uint8_t *)(pReqMsg->shmAddr) + pReqMsg->msgOffset + sizeof(cmsMsg_header_t);
	pReqMsg->pCurMsgCb = pReqMsg;
	pReqMsg->next = NULL;
	
	memset(pReqMsg->shmAddr, 0, shmSize);


	if(pReqMsg->header.srcModuleId == (unsigned int)-1){
		CMS_LOG(LL_FATAL,"create request msg fail, this module has not register to cms_core or has deregister to cms_core\n");
		//return NULL;
	}
	
	CMS_LOG(LL_DEBUG,"Exit, create request msg success. srcId %d, dtsId %d, sessionId %d, actionNum %d, flag: 0x%x, shmId %d.\n",
		pReqMsg->header.srcModuleId, pReqMsg->header.dstModuleId,pReqMsg->header.sessionId,
		pReqMsg->header.actionNum, pReqMsg->header.flag, pReqMsg->shmId);

	return pReqMsg;
}

cmsMsgCb_t* cms_createReplyMsg(cmsMsgCb_t *pMsg, uint32_t status){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	
	cmsMsgCb_t *pCurMsg = NULL, *pReplyMsg = NULL, *pNextPeplyMsg = NULL, *pTmpMsg = NULL;
	
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "pMsg is NULL, create reply msg fail.\n");
		return NULL;
	}

	pReplyMsg = (cmsMsgCb_t *)calloc(1, sizeof(cmsMsgCb_t));
	if(pReplyMsg == NULL){
		CMS_LOG(LL_ERROR, "malloc msgcb fail, create reply msg fail.\n");
		return NULL;
	}

	pCurMsg = pMsg;
	memcpy(pReplyMsg, pCurMsg, sizeof(cmsMsgCb_t));
	pReplyMsg->pCurMsgCb = pReplyMsg;
	pReplyMsg->msgType = 0x0;
	pReplyMsg->msgType |=  CMS_MSG_SEND;
	pReplyMsg->next = NULL;
	pCurMsg->shmAddr = NULL;
	pCurMsg->msgType = 0x0;
	pCurMsg->msgType |= CMS_MSG_SEND; 

	while(pCurMsg->next != NULL){
		/*create new reply msg for next node*/
		pNextPeplyMsg = (cmsMsgCb_t *)calloc(1, sizeof(cmsMsgCb_t));
		memcpy(pNextPeplyMsg, pCurMsg->next, sizeof(cmsMsgCb_t));
		pNextPeplyMsg->pCurMsgCb = pNextPeplyMsg;
		pNextPeplyMsg->msgType |= CMS_MSG_SEND;
		pNextPeplyMsg->next = NULL;

		/*add new msg to list tail*/
		pTmpMsg = pReplyMsg;
		while(pTmpMsg->next != NULL){
			pTmpMsg = pTmpMsg->next;
		}
		pTmpMsg->next = pNextPeplyMsg;

		pCurMsg = pCurMsg->next;
		pCurMsg->shmAddr = NULL;
	}
	
	pReplyMsg->header.srcModuleId = pMsg->header.dstModuleId;
	pReplyMsg->header.dstModuleId = pMsg->header.srcModuleId;
	pReplyMsg->header.msgType = pMsg->header.msgType;
	pReplyMsg->header.flag &= ~ CMS_MSG_REQUEST;
	pReplyMsg->header.flag |= CMS_MSG_REPLY;
	if(status == CMS_STATUS_SUCCESS){
		SET_ACK(pReplyMsg->header.flag);
	}else{
		SET_NACK(pReplyMsg->header.flag);
	}
	pReplyMsg->header.sessionId = pMsg->header.sessionId;
	
	pReplyMsg->pCurData = (uint8_t *)(pReplyMsg->shmAddr) + pReplyMsg->msgOffset + sizeof(cmsMsg_header_t);
	
	CMS_LOG(LL_DEBUG, "Exit, replay msg shmid: %d, recv msg sid %d\n",
		pReplyMsg->shmId, pMsg->header.srcModuleId);
	return pReplyMsg;
}

int cms_sendMsg(cmsMsgCb_t *pMsg, uint32_t dtsModuleId){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	
	cmsMsgCb_t *pCurMsg = NULL;
	cmsMsqBuf_t msqBuf;
	int Key = -1;
	int msqId = 0;
	int ret = 0;
	int i = 0;

	
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "pMsg is NULL, send msg fail.\n");
		return -1;
	}

	/*init msqbuf*/
	memset(&msqBuf, 0, sizeof(cmsMsqBuf_t));
	msqBuf.shmNum = 0;

	if(pMsg->header.msgType == 0){
		srand(gettid());//seed of rand
		msqBuf.mType = rand(); //define need reply send Msg type.
		pMsg->header.msgType = msqBuf.mType;
	}else{
		msqBuf.mType = pMsg->header.msgType; //define need reply send Msg type.
	}

	clock_gettime(CLOCK_MONOTONIC, &(pMsg->header.timeStamp));

	/*compute len in each msgCb*/
	pCurMsg = pMsg;
	while(pCurMsg != NULL){
		pCurMsg->length = pCurMsg->pCurData - pCurMsg->shmAddr - pCurMsg->msgOffset;
		pCurMsg = pCurMsg->next;
	}
	pMsg->pCurMsgCb = pMsg;
	pMsg->pCurData = (uint8_t *)(pMsg->shmAddr) + pMsg->msgOffset;
	/*copy header to shm*/
	*((cmsMsg_header_t*)pMsg->pCurData) = pMsg->header;

	CMS_LOG(LL_DEBUG, "before send msg, srcId: %d, dtsId: %d, sessionId: %d, actNum: %d, shmid : %d\n",
		pMsg->header.srcModuleId,
		pMsg->header.dstModuleId,pMsg->header.sessionId,
		pMsg->header.actionNum, pMsg->shmId);


	pCurMsg = pMsg;
	i = 0;
	/*add shm id in msqbuf*/
	while(pCurMsg != NULL){
		msqBuf.shmId[i] = pCurMsg->shmId;
		*((uint32_t *)pCurMsg->shmAddr) = pCurMsg->length;

		CMS_LOG(LL_NOTICE, "detch shm addr %d, id %d\n", pCurMsg->shmId,msqBuf.shmId[i]);
		if(shmdt(pCurMsg->shmAddr) == -1){
			CMS_LOG(LL_NOTICE, "share memory detach fail.\n");
		}else{
			CMS_LOG(LL_NOTICE, "share memory detach success.\n");
		}
		
		pCurMsg->shmAddr = NULL;
		pCurMsg = pCurMsg->next;

		i++;
	}

	msqBuf.shmNum = i;
	
	CMS_LOG(LL_DEBUG, "ready to send msg,shm num %d, shmid %d.\n", msqBuf.shmNum, msqBuf.shmId[0]);
	/*send msg*/
	pMsg->header.dstModuleId = dtsModuleId;
	if(dtsModuleId == 0){
		genKeyByName(CMS_CORE, &Key, NULL);
		msqId = msgget(Key, 0666);
		if(msqId == -1){
			msqId = msgget(Key, 0666 | IPC_CREAT);
		}else{
			CMS_LOG(LL_DEBUG, "msgget success, msqId %d, key 0x%x.\n", msqId, Key);
		}
		if(msqId == -1){
			CMS_LOG(LL_ERROR, "msgget fail, error is %s.\n", strerror(errno));
			return -1;
		}else{
			ret = msgsnd(msqId, &msqBuf, sizeof(msqBuf)-sizeof(long), IPC_NOWAIT);
		}	
	}else{
		ret = msgsnd(dtsModuleId, &msqBuf, sizeof(msqBuf)-sizeof(long), IPC_NOWAIT);
	}

	if(ret < 0){
		CMS_LOG(LL_ERROR, "Exit, shmsnd fail, error is %s.\n",strerror(errno));
		return -1;
	}else{
		CMS_LOG(LL_INFO, "Exit, shmsnd success.\n");
		return msqBuf.mType;
	}
}

int cms_sendMsgNoReply(cmsMsgCb_t *pMsg, uint32_t dtsModuleId)
{
	CMS_LOG(LL_DEBUG, "Extrance\n");
	
	cmsMsgCb_t *pCurMsg = NULL;
	cmsMsqBuf_t msqBuf;
	int Key = -1;
	int	msqId = 0;
	int ret = 0;
	int i = 0;

	
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "pMsg is NULL, send msg fail.\n");
		return -1;
	}

	/*init msqbuf*/
	memset(&msqBuf, 0, sizeof(cmsMsqBuf_t));
	msqBuf.shmNum = 0;
	msqBuf.mType = 1; //define no reply send Msg type.

	/*compute len in each msgCb*/
	pCurMsg = pMsg;
	while(pCurMsg != NULL){
		pCurMsg->length = pCurMsg->pCurData - pCurMsg->shmAddr - pCurMsg->msgOffset;
		pCurMsg = pCurMsg->next;
	}
	pMsg->pCurMsgCb = pMsg;
	pMsg->pCurData = (uint8_t *)(pMsg->shmAddr) + pMsg->msgOffset;
	pMsg->header.flag |= CMS_MSG_NOREPLY;
	/*copy header to shm*/
	*((cmsMsg_header_t*)pMsg->pCurData) = pMsg->header;

	CMS_LOG(LL_DEBUG, "before send msg, srcId: %d, dtsId: %d, sessionId: %d, actNum: %d, shmid : %d\n",
		pMsg->header.srcModuleId,
		pMsg->header.dstModuleId,pMsg->header.sessionId,
		pMsg->header.actionNum, pMsg->shmId);


	pCurMsg = pMsg;
	i = 0;
	/*add shm id in msqbuf*/
	while(pCurMsg != NULL){
		msqBuf.shmId[i] = pCurMsg->shmId;
		*((uint32_t *)pCurMsg->shmAddr) = pCurMsg->length;

		CMS_LOG(LL_NOTICE, "detch shm addr %d, id %d\n", pCurMsg->shmId,msqBuf.shmId[i]);
		if(shmdt(pCurMsg->shmAddr) == -1){
			CMS_LOG(LL_NOTICE, "share memory detach fail.\n");
		}else{
			CMS_LOG(LL_NOTICE, "share memory detach success.\n");
		}
		
		pCurMsg->shmAddr = NULL;
		pCurMsg = pCurMsg->next;

		i++;
	}

	msqBuf.shmNum = i;
	
	CMS_LOG(LL_DEBUG, "ready to send msg,shm num %d, shmid %d.\n", msqBuf.shmNum, msqBuf.shmId[0]);
	/*send msg*/
	pMsg->header.dstModuleId = dtsModuleId;
	if(dtsModuleId == 0){
		genKeyByName(CMS_CORE, &Key, NULL);
		msqId = msgget(Key, 0666);
		if(msqId == -1){
			msqId = msgget(Key, 0666 | IPC_CREAT);
		}else{
			CMS_LOG(LL_DEBUG, "msgget success, msqId %d, key 0x%x.\n", msqId, Key);
		}
		if(msqId == -1){
			CMS_LOG(LL_ERROR, "msgget fail, error is %s.\n", strerror(errno));
			return -1;
		}else{
			ret = msgsnd(msqId, &msqBuf, sizeof(msqBuf)-sizeof(long), IPC_NOWAIT);
		}	
	}else{
		ret = msgsnd(dtsModuleId, &msqBuf, sizeof(msqBuf)-sizeof(long), IPC_NOWAIT);
	}

	if(ret < 0){
		CMS_LOG(LL_ERROR, "Exit, shmsnd fail, error is %s.\n",strerror(errno));
		return -1;
	}else{
		CMS_LOG(LL_INFO, "Exit, shmsnd success.\n");
		return 0;
	}
}


cmsMsgCb_t* cms_recvMsg(uint32_t timeoutMs, long msgType){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsqBuf_t msqBuf;
	cmsMsgCb_t *pFirstMsg = NULL, *pNewMsg = NULL, *pCurMsg = NULL, *pTmpMsg = NULL;
	int polling_interval_ms = 0;
	int ret = 0;
	int i = 0;
	int memLeak = 0;

	memset(&msqBuf, 0, sizeof(cmsMsqBuf_t));
	
	/*recv msg in timeout range*/
	if(timeoutMs > 0){
		if(timeoutMs < MSGRECV_POLLING_INTERVAL_MS)
			polling_interval_ms = MSGRECV_POLLING_INTERVAL_MS;
		else
			polling_interval_ms = timeoutMs;

		do{
			if(msgType == CMS_MSG_TYPE)//when stats msg, recv all type with 0.
				ret = msgrcv(cmsStatsId, &msqBuf, sizeof(cmsMsqBuf_t)-sizeof(long),0, IPC_NOWAIT);
			else
				ret = msgrcv(cmsModuleId, &msqBuf, sizeof(cmsMsqBuf_t)-sizeof(long),msgType, IPC_NOWAIT);
			//CMS_LOG(LL_DEBUG, "[%s-%d], after recv, ret is %d, shm num is %d, id %d\n", __FUNCTION__, __LINE__, ret, msqBuf.shmNum, msqBuf.shmId[0]);
			
			if((polling_interval_ms < MSGRECV_POLLING_INTERVAL_MS) || (ret > 0))
				break;

			usleep(MSGRECV_POLLING_INTERVAL_MS*1000);
			
			polling_interval_ms = polling_interval_ms - MSGRECV_POLLING_INTERVAL_MS;
		}while((ret == -1) && ((errno == ENOMSG) || (errno == EINTR)));
	}else{
		if(msgType == CMS_MSG_TYPE)//when stats msg, recv all type with 0.
			ret = msgrcv(cmsStatsId, &msqBuf, sizeof(cmsMsqBuf_t)-sizeof(long),0, 0);
		else
			ret = msgrcv(cmsModuleId, &msqBuf, sizeof(cmsMsqBuf_t)-sizeof(long),msgType, 0);
		CMS_LOG(LL_DEBUG, "after recv, shm num is %d, id %d\n", msqBuf.shmNum, msqBuf.shmId[0]);
	}
	
	/*handle msg*/
	if(ret > 0 && (msqBuf.shmNum > 0)){
		do{
			if(i == 0){//first cb
				if((pFirstMsg = (cmsMsgCb_t *)calloc(1, sizeof(cmsMsgCb_t))) == NULL){
					CMS_LOG(LL_ERROR, "calloc msgcb fail, error is %s.\n", strerror(errno));
					memLeak = 1;
					break;
				}
				pFirstMsg->shmId = msqBuf.shmId[i];
				pFirstMsg->shmAddr = (void *)shmat(pFirstMsg->shmId, 0, 0);
				pFirstMsg->msgOffset = MSG_OFFSET;
				pFirstMsg->length = *((uint32_t *)pFirstMsg->shmAddr);
				CMS_LOG(LL_DEBUG,"the shm addr is %p==============================\n", pFirstMsg->shmAddr);
				pFirstMsg->pCurData = (uint8_t *)(pFirstMsg->shmAddr) + pFirstMsg->msgOffset + sizeof(cmsMsg_header_t);
				pFirstMsg->pCurMsgCb = pFirstMsg;
				pFirstMsg->next = NULL;

				CMS_LOG(LL_INFO, "recv shm addr 0x%p, shmid %d\n", pFirstMsg->shmAddr, pFirstMsg->shmId);
				memcpy(&(pFirstMsg->header), (uint8_t *)(pFirstMsg->shmAddr) + pFirstMsg->msgOffset, sizeof(cmsMsg_header_t));
				pFirstMsg->msgType |= CMS_MSG_RECV;
			}else{
				if((pNewMsg = (cmsMsgCb_t *)calloc(1, sizeof(cmsMsgCb_t))) == NULL){
					CMS_LOG(LL_ERROR, "calloc new msgcb fail, error is %s.\n", strerror(errno));

					pCurMsg = pFirstMsg;
					while(pCurMsg != NULL){
						shmdt(pCurMsg->shmAddr);
						pTmpMsg = pCurMsg;
						pCurMsg = pCurMsg->next;

						safe_free(pTmpMsg);
					}
					memLeak = 1;
					break;
				}
				pNewMsg->shmId = msqBuf.shmId[i];
				pNewMsg->shmAddr = (void *)shmat(pNewMsg->shmId, 0, 0);
				pNewMsg->msgOffset = MSG_OFFSET;
				pNewMsg->length = *((uint32_t *)pNewMsg->shmAddr);
				pNewMsg->pCurData = (uint8_t *)(pNewMsg->shmAddr) + pNewMsg->msgOffset;
				pNewMsg->next = NULL;

				pCurMsg = pFirstMsg;
				while(pCurMsg->next != NULL){
					pCurMsg = pCurMsg->next;
				}
				pCurMsg->next = pNewMsg;
			}
			
			if((i++) >= msqBuf.shmNum)
				break;

		}while(msqBuf.shmId[i] != 0);

		if(memLeak == 1){
			i = 0;
			while(msqBuf.shmId[i] != 0){
				shmctl(msqBuf.shmId[i], IPC_RMID, NULL);
				i++;
			}

			//cmsSessionId--;
			return NULL;
		}
		
		//if(pFirstMsg->header.flag & CMS_MSG_REPLY)
			//cmsSessionId--;

	}else{
		//CMS_LOG(LL_ERROR, "Exit, shm rev fail, error is %s, ret %d, shmNum %d.\n", strerror(errno), ret, msqBuf.shmNum);
		return NULL;
	}

	CMS_LOG(LL_DEBUG, "Exit\n");

	return pFirstMsg;
}

int cms_recvMsgResult(cmsMsgCb_t *pReqMsg, uint32_t timeoutMs, cmsStatsBuf_t *statsBuf, long msgType){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	int polling_interval_ms = 0;
	int recvFlag = 0;
	int ret = 0;
	int result = CMS_SENDOK;

	memset(statsBuf, 0, sizeof(cmsStatsBuf_t));
	
	/*recv msg in timeout range*/
	if(timeoutMs > 0){
		if(timeoutMs < MSGRECV_POLLING_INTERVAL_MS)
			polling_interval_ms = MSGRECV_POLLING_INTERVAL_MS;
		else
			polling_interval_ms = timeoutMs;

		do{
			ret = msgrcv(cmsModuleId, statsBuf, sizeof(cmsStatsBuf_t)-sizeof(long),msgType, IPC_NOWAIT);
			if(ret != -1){
				recvFlag = 1;
				if((statsBuf->timeStamp.tv_sec == pReqMsg->header.timeStamp.tv_sec) && (statsBuf->timeStamp.tv_nsec == pReqMsg->header.timeStamp.tv_nsec)){
					if(statsBuf->statsRet == CMS_STATUS_SUCCESS)
						result = CMS_OK;
					if(statsBuf->statsRet == CMS_STATUS_FAIL)
						result = CMS_RESPONSEFAIL;
					break;  
				}else{
					result = CMS_RECVOTHER;
				}
			}

			if(polling_interval_ms < MSGRECV_POLLING_INTERVAL_MS)
				break;

			usleep(MSGRECV_POLLING_INTERVAL_MS*1000);
			
			polling_interval_ms = polling_interval_ms - MSGRECV_POLLING_INTERVAL_MS;
		}while(((ret == -1) && ((errno == ENOMSG)||(errno == EINTR))) || (result == CMS_RECVOTHER));

		if(recvFlag == 0)
			result = CMS_SENDOK;
	}else{
		ret = msgrcv(cmsModuleId, statsBuf, sizeof(cmsStatsBuf_t)-sizeof(long),msgType, 0);
		if(ret != -1){
			if((statsBuf->timeStamp.tv_sec == pReqMsg->header.timeStamp.tv_sec) && (statsBuf->timeStamp.tv_nsec == pReqMsg->header.timeStamp.tv_nsec)){
				if(statsBuf->statsRet == CMS_STATUS_SUCCESS)
					result = CMS_OK;
				if(statsBuf->statsRet == CMS_STATUS_FAIL)
					result = CMS_RESPONSEFAIL;
			}else{
				result = CMS_RECVOTHER;
			}
		}else{
			CMS_LOG(LL_ERROR, "msg recv fail, error code %d(%s)",  errno, strerror(errno));
		}
	}

	CMS_LOG(LL_DEBUG, "Exit\n");

	return result;
}


/*reponse msg pointer, use in get info from cms_db*/
cmsMsgCb_t* cms_SendRecvMsg(cmsMsgCb_t *pReqMsg, uint32_t dstModuleId, uint32_t timeoutMs){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *recvMsg = NULL;
	int ret = -1;
	
	if(pReqMsg == NULL){
		CMS_LOG(LL_ERROR, "pReqMsg is NULL.\n");
		return NULL;
	}

	ret = cms_sendMsg(pReqMsg, dstModuleId);
	if(ret < 0){
		CMS_LOG(LL_ERROR, "send msg fail.\n");
		return NULL;
	}
	
	recvMsg = cms_recvMsg(timeoutMs, ret);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return recvMsg;
}

/*only reponse ok or nok, use in set to db*/
int cms_SendRecvMsgResult(cmsMsgCb_t *pReqMsg, uint32_t dstModuleId, uint32_t timeoutMs)
{
	CMS_LOG(LL_DEBUG, "Extrance\n");
	int ret = CMS_NOK;
	cmsStatsBuf_t statsBuf;
	struct msqid_ds msgQueue;
	long msgType;
	
	if(pReqMsg == NULL){
		CMS_LOG(LL_ERROR, "pReqMsg is NULL.\n");
		return CMS_NOK;
	}


	/*check queue will full, clean the msg queue; else clean with the same queue type msg*/
	memset(&msgQueue, 0, sizeof(struct msqid_ds));
	if(msgctl(cmsModuleId, IPC_STAT, &msgQueue) == -1){
		CMS_LOG(LL_ERROR, "get msg queue fai, error code %d(%s).\n", errno ,strerror(errno));
		return CMS_NOK;
	}

	if(msgQueue.msg_qnum >= MSG_QUEUE_MAX_NUM ){
		do{
			ret = msgrcv(cmsModuleId, &statsBuf, sizeof(cmsStatsBuf_t)-sizeof(long),0, IPC_NOWAIT);
			CMS_LOG(LL_ERROR, "msg queue is full, recving old msg, current msgNum %lu.\n", msgQueue.msg_qnum);
		}while(ret != -1);
	}else{
		if(pReqMsg->header.msgType == 0){
			srand(gettid());//seed of rand
			pReqMsg->header.msgType = rand();//define need reply send Msg type.
		}
		
		do{
			ret = msgrcv(cmsModuleId, &statsBuf, sizeof(cmsStatsBuf_t)-sizeof(long), pReqMsg->header.msgType, IPC_NOWAIT);
			CMS_LOG(LL_DEBUG, "msg queue is not full(max %lu, current %lu), recv same type %ld, ret %d.\n", 
				msgQueue.msg_qbytes, msgQueue.msg_cbytes, pReqMsg->header.msgType, ret);
		}while(ret != -1);
	}

	/*send & recv msg*/
	if((msgType = cms_sendMsg(pReqMsg, dstModuleId)) < 0){
		CMS_LOG(LL_ERROR, "send msg fail.\n");
		return CMS_NOK;
	}

	ret = CMS_SENDOK; 
	
	ret = cms_recvMsgResult(pReqMsg, timeoutMs, &statsBuf, msgType);
	if(ret == CMS_SENDOK)
		CMS_LOG(LL_ERROR, "cms msg recv fail, msg reply timeout.\n");
	else if(ret == CMS_RECVOTHER)
		CMS_LOG(LL_ERROR, "cms msg recv not correspond, msg reply timeout.\n");
	else if(ret == CMS_RESPONSEFAIL)
		CMS_LOG(LL_ERROR, "cms action fail, msg handle fail, reply fail info.\n");
	
	CMS_LOG(LL_DEBUG, "Exit\n");
	return ret;
}



#if 0
int	cms_subcribeEvent(uint32_t numOfEvent, const uint32_t *eventId, void (*sigHandle)(int sigNum, siginfo_t* sigInfo, void *ptr)){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pReqMsg = NULL, *pRecvMsg = NULL;
	struct sigaction sigEvent;

	/*create new request msg parameter*/
	if((pReqMsg = cms_createRequestMsg(0)) == NULL)
		return -1;

	/*regist signall to wait event incoming*/
	sigEvent.sa_sigaction = sigHandle;
	sigemptyset(&sigEvent.sa_mask);
	sigEvent.sa_flags = SA_SIGINFO;
	sigEvent.sa_restorer = NULL;

	if(sigaction(SIGEVENT, &sigEvent, NULL) < 0){
		CMS_LOG(LL_ERROR, "send signal fail.\n");
		cms_msgFree(pReqMsg);
		return -1;
	}

	/*fill event info to pReq msg*/
	cms_requestSubscribeEvent(pReqMsg, numOfEvent, eventId);

	pRecvMsg = cms_SendRecvMsg(pReqMsg, 0, 60000);

	/*check recv msg, flag*/
	if(pRecvMsg != NULL){
		if(IS_NACK(pRecvMsg->header.flag)){
			CMS_LOG(LL_ERROR, "recv msg fail.\n");
			cms_msgFree(pReqMsg);
			cms_msgFree(pRecvMsg);
			return -1;
		}
		cms_msgFree(pReqMsg);
		cms_msgFree(pRecvMsg);
	}else{
		CMS_LOG(LL_ERROR, "send & recv msg fail.\n");
		cms_msgFree(pReqMsg);
		return -1;
	}

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}
#else
int	cms_subcribeEvent(uint32_t numOfEvent, const uint32_t *eventId, void* (*event_rtn)(void *), void *arg){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pReqMsg = NULL, *pRecvMsg = NULL;
	int ret = -1;

	/*create new request msg parameter*/
	if((pReqMsg = cms_createRequestMsg(0)) == NULL)
		return -1;

	/*fill event info to pReq msg*/
	cms_requestSubscribeEvent(pReqMsg, numOfEvent, eventId);

	pRecvMsg = cms_SendRecvMsg(pReqMsg, 0, 60000);

	/*check recv msg, flag*/
	if(pRecvMsg != NULL){
		if(IS_NACK(pRecvMsg->header.flag)){
			CMS_LOG(LL_ERROR, "recv msg fail.\n");
			cms_msgFree(pReqMsg);
			cms_msgFree(pRecvMsg);
			return -1;
		}
		cms_msgFree(pReqMsg);
		cms_msgFree(pRecvMsg);
	}else{
		CMS_LOG(LL_ERROR, "send & recv msg fail.\n");
		cms_msgFree(pReqMsg);
		return -1;
	}

	pthread_attr_init(&threadAttr);
	pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED);

	ret = pthread_create(&cmsThreadHandle, &threadAttr, event_rtn, arg); 
	if(ret != 0){
		CMS_LOG(LL_ERROR, "cms event thread create fail.\n");
		return -1;
	}

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}
#endif

int cms_unSubcribeEvent(uint32_t numOfEvent, const uint32_t *eventId){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pReqMsg = NULL, *pRecvMsg = NULL;

	/*create new request msg parameter*/
	if((pReqMsg = cms_createRequestMsg(0)) == NULL)
		return -1;

	/*fill event info to pReq msg*/
	cms_requestUnsubscribeEvent(pReqMsg, numOfEvent, eventId);

	pRecvMsg = cms_SendRecvMsg(pReqMsg, 0, 500);

	/*check recv msg falg */
	if(pRecvMsg != NULL){
		if(IS_NACK(pRecvMsg->header.flag)){
			CMS_LOG(LL_ERROR, "recv msg fail, nack.\n");
			cms_msgFree(pReqMsg);
			cms_msgFree(pRecvMsg);
			return -1;
		}
		cms_msgFree(pReqMsg);
		cms_msgFree(pRecvMsg);
	}else{
		CMS_LOG(LL_ERROR, "send & recv msg fail.\n");
		cms_msgFree(pReqMsg);
		return -1;
	}

	pthread_attr_destroy(&threadAttr);
	//pthread_join(cmsThreadHandle, NULL);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

void cms_threadEventSigBlock(void){
	sigset_t threadSigSet;

	sigemptyset(&threadSigSet);
	sigaddset(&threadSigSet, SIGEVENT);
	
	pthread_sigmask(SIG_BLOCK, &threadSigSet, NULL);
}

objParaList_t *cms_getObject(uint32_t objId, uint64_t objIA){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pReqMsg = NULL, *pRecvMsg = NULL, *objParaMsg = NULL;
	cmsMsg_action_t *pAction = NULL, *paraAction = NULL;
	objParaList_t *paraList = NULL, *curParaList = NULL, *newParaList = NULL;
	uint32_t actionNum = 0, paraNum = 0;
	int dbMsqId = 0;
	int dbKey = 0;

	/*creat msg and request get object parameter*/
	pReqMsg = cms_createRequestMsg(0);
	if(pReqMsg == NULL){
		CMS_LOG(LL_ERROR,"get object fail, create request msg fail.\n");
		return NULL;
	}

	cms_requestGetObject(pReqMsg, objId, objIA);
	genKeyByName(CMS_DB, &dbKey, NULL);
	if((dbMsqId = msgget(dbKey, 0666)) == -1){
		cms_msgFree(pReqMsg);
		return NULL;
	}
	
	pRecvMsg = cms_SendRecvMsg(pReqMsg,dbMsqId,500);
	cms_msgFree(pReqMsg);

	/*parse reponse msg*/
	if(pRecvMsg != NULL){
		actionNum = pRecvMsg->header.actionNum;
		while(actionNum){
			pAction = cms_parseMsgAction(pRecvMsg);

			if(pAction->action->type == CMS_MSG_ACT_PUT_PARAINFO){
				CMS_LOG(LL_DEBUG, "cur para shmid is %d\n",pAction->objDataShm->objChgShmId);

				/*init parameters msg, the msg stored obj parameter info*/
				objParaMsg = (cmsMsgCb_t *)calloc(1, sizeof(cmsMsgCb_t));
				objParaMsg->shmId = pAction->objDataShm->objChgShmId;

				printf("2shmid of para is %d============================\n", objParaMsg->shmId);
				objParaMsg->shmAddr = (void *)shmat(objParaMsg->shmId, 0, 0);
				objParaMsg->msgOffset = MSG_OFFSET;
				objParaMsg->msgType |= CMS_MSG_RECV;
				objParaMsg->length = 0;
				objParaMsg->pCurData = (uint8_t *)(objParaMsg->shmAddr) + objParaMsg->msgOffset + sizeof(cmsMsg_header_t);
				objParaMsg->pCurMsgCb = objParaMsg;
				objParaMsg->next = NULL;
				memcpy(&(objParaMsg->header), (uint8_t *)(objParaMsg->shmAddr) + objParaMsg->msgOffset, sizeof(cmsMsg_header_t));

				/*parse obj parameter msg*/
				paraNum = objParaMsg->header.actionNum;
				CMS_LOG(LL_DEBUG,"para num is %d\n", paraNum);
				while(paraNum){
					paraAction = cms_parseMsgAction(objParaMsg);
					if(paraAction->action->type == CMS_MSG_ACT_PARAMETERINFO){
						
						/*if malloc fail, release list*/
						newParaList = (objParaList_t *)calloc(1, sizeof(objParaList_t));
						if(newParaList == NULL){
							objParaList_t *tmpParaList = NULL;
							curParaList = paraList;
							while(curParaList){
								tmpParaList = curParaList;
								curParaList = curParaList->next;

								DELETE_NODE(paraList, tmpParaList);
							}
							cms_msgFree(pRecvMsg);
							cms_msgFree(objParaMsg);
							CMS_LOG(LL_ERROR,"get object fail, calloc newObjList fail, no mem.\n");
							return NULL;
						}

						/*assign new parainfo value*/
						memcpy(newParaList->parameter.parameterName,paraAction->paraAttr->parameter.parameterName, paraAction->paraAttr->parameter.sizeofParameter);
						newParaList->parameter.typeSizeOfValue = paraAction->paraValue->typeSizeOfValue;
						if((newParaList->parameter.typeSizeOfValue &PARAMETER_TYPE_STRING) == PARAMETER_TYPE_STRING)
							memcpy(newParaList->parameter.paraValue, paraAction->paraValue->value, (strlen((char *)paraAction->paraValue->value)+1));
						else
							memcpy(newParaList->parameter.paraValue, paraAction->paraValue->value, (paraAction->paraValue->typeSizeOfValue & ~((uint32_t)PARAMETER_TYPE_CLASS_BITS)));
						newParaList->flag = 0;
						newParaList->next = NULL;
						newParaList->prev = NULL;

						/*insert to list tail*/
						INSERT_NODE(paraList,newParaList);
			
			
						#if 0 /*debug print*/
						curParaList = paraList;
						while(curParaList){
							if(curParaList->parameter.typeSizeOfValue == PARAMETER_TYPE_STRING){
								printf("paraname: %s, paravalue: %s, typesizeofValue %d, attr %d, active %d, flag %d.\n",
								curParaList->parameter.parameterName, curParaList->parameter.paraValue,curParaList->parameter.typeSizeOfValue,
								curParaList->parameter.attr, curParaList->parameter.active, curParaList->parameter.flag);
							}
							else if(curParaList->parameter.typeSizeOfValue == PARAMETER_TYPE_UINT8){
								printf("paraname: %s, paravalue: %d, typesizeofValue %d, attr %d, active %d, flag %d.\n",
								curParaList->parameter.parameterName, *(uint8_t *)(curParaList->parameter.paraValue),curParaList->parameter.typeSizeOfValue,
								curParaList->parameter.attr, curParaList->parameter.active, curParaList->parameter.flag);
							}
							else if(curParaList->parameter.typeSizeOfValue == PARAMETER_TYPE_BOOLEAN){
								printf("paraname: %s, paravalue: %d, typesizeofValue %d, attr %d, active %d, flag %d.\n",
								curParaList->parameter.parameterName, *(uint8_t *)(curParaList->parameter.paraValue),curParaList->parameter.typeSizeOfValue,
								curParaList->parameter.attr, curParaList->parameter.active, curParaList->parameter.flag);
							}
							else if(curParaList->parameter.typeSizeOfValue == PARAMETER_TYPE_UINT16){
								printf("paraname: %s, paravalue: %d, typesizeofValue %d, attr %d, active %d, flag %d.\n",
								curParaList->parameter.parameterName, *(uint16_t *)(curParaList->parameter.paraValue),curParaList->parameter.typeSizeOfValue,
								curParaList->parameter.attr, curParaList->parameter.active, curParaList->parameter.flag);
							}
							else if(curParaList->parameter.typeSizeOfValue == PARAMETER_TYPE_UINT32){
								printf("paraname: %s, paravalue: %d, typesizeofValue %d, attr %d, active %d, flag %d.\n",
								curParaList->parameter.parameterName, *(uint32_t *)(curParaList->parameter.paraValue),curParaList->parameter.typeSizeOfValue,
								curParaList->parameter.attr, curParaList->parameter.active, curParaList->parameter.flag);
							}
							
							curParaList = curParaList->next;
						}
						#endif
					}
					
					paraNum--;
				}

				cms_msgFree(objParaMsg);
			}
			else{
				CMS_LOG(LL_ERROR, "receive error msg code\n");
			}
			actionNum--;
		}
		
		cms_msgFree(pRecvMsg);
	}

	
	CMS_LOG(LL_DEBUG, "Exit\n");
	return paraList;
}


int cms_setObjectByPathName(char *pathName, objParaList_t *paraList){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pReqMsg = NULL;
	objParaList_t *curParaList = NULL;
	int dbMsqId = 0;
	int dbKey = 0;
	int isChanged = 0;
	
	if(paraList == NULL){
		CMS_LOG(LL_ERROR,"set object fail, objList is NUll, free another not NULL list.\n");
		return -1;
	}

	pReqMsg = cms_createRequestMsg(0);
	if(pReqMsg == NULL){
		CMS_LOG(LL_ERROR,"set object fail, create request msg fail.\n");
		return -1;
	}

	cms_requestSelObjByName(pReqMsg, pathName);
	
	/*check if parameter has been changed*/
	curParaList = paraList;
	while(curParaList){
		if(curParaList->flag == 1 ){
			isChanged = 1;
			curParaList->flag = 0;
			
			cms_requestSetPara(pReqMsg, curParaList->parameter.parameterName, 
				curParaList->parameter.typeSizeOfValue, curParaList->parameter.paraValue);

		}

		curParaList = curParaList->next;
	}

	if(isChanged == 1){
		genKeyByName(CMS_DB, &dbKey, NULL);
		if((dbMsqId = msgget(dbKey, 0666)) == -1){
			cms_msgFree(pReqMsg);
			return -1;
		}

		int ret = cms_SendRecvMsgResult(pReqMsg,dbMsqId,500);
		if(ret == CMS_NOK){
			CMS_LOG(LL_ERROR,"send msg fail !!\n");
			cms_msgAndShmFree(pReqMsg);
			return -1;
		}else{
			cms_msgFree(pReqMsg);
			if(ret != CMS_OK){
				CMS_LOG(LL_ERROR,"recv msg fail !!\n");
			}
			return 0;
		}
	}
	
	cms_msgAndShmFree(pReqMsg);
	
	CMS_LOG(LL_DEBUG, "Exit");
	return 0;
}

objParaList_t *cms_createObjectParaList(void){
	objParaList_t *paraListHead = NULL;

	paraListHead = (objParaList_t *)calloc(1, sizeof(objParaList_t));
	if(paraListHead == NULL){
		CMS_LOG(LL_ERROR,"malloc parameter list fail.\n");
		return NULL;
	}
	paraListHead->flag = 0;

	return paraListHead;
}


int cms_freeObjectParaList(objParaList_t *paraList){
	objParaList_t *curPara = NULL, *tmpPara = NULL;

	if(paraList == NULL){
		CMS_LOG(LL_DEBUG, "para list is NULL.\n");
		return 0;
	}
	
	curPara = paraList;
	while(curPara){
		tmpPara = curPara;
		curPara = curPara->next;

		free(tmpPara->parameter.paraValue);
		tmpPara->parameter.paraValue = NULL;
		DELETE_NODE(paraList,tmpPara);
	}

	return 0;
}

int cms_setParameterToList(objParaList_t *paraList, char *paraName, uint32_t typeOfValue, void *value){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	objParaList_t *curParaList = NULL, *newParaList = NULL;
	int isFound = -1;
	int valueLen = 0;
	
	if((paraList == NULL) || (paraName == NULL)){
		CMS_LOG(LL_ERROR,"set parameter fail, paraList is NUll, free another not NULL list.\n");
		return -1;
	}
	
	curParaList = paraList;
	while(curParaList){
		if(!strcmp(curParaList->parameter.parameterName, paraName)){
	
			if(strcmp((char *)curParaList->parameter.paraValue, value)){

				free(curParaList->parameter.paraValue);

				if(typeOfValue == PARAMETER_TYPE_STRING){
					valueLen = strlen(value) + 1;
				}else{
					valueLen = (typeOfValue & ~((uint32_t)PARAMETER_TYPE_CLASS_BITS));
				}
				curParaList->parameter.paraValue = (uint8_t *)calloc(valueLen, sizeof(uint8_t));
				if(curParaList->parameter.paraValue == NULL){
					CMS_LOG(LL_ERROR, "calloc parameter value fail.\n");
					return -1;
				}
					
				strcpy((char *)curParaList->parameter.paraValue, value);
			}

			curParaList->parameter.typeSizeOfValue = typeOfValue;

			curParaList->flag = 1;
			
			isFound = 1;
			break;
		}

		curParaList = curParaList->next;
	}

	if(isFound !=  1){
		if(paraList->flag == 0){
			if(typeOfValue == PARAMETER_TYPE_STRING){
				valueLen = strlen(value) + 1;
			}else{
				valueLen = (typeOfValue & ~((uint32_t)PARAMETER_TYPE_CLASS_BITS));
			}
			paraList->parameter.paraValue = (uint8_t *)calloc(valueLen, sizeof(uint8_t));
			if(paraList->parameter.paraValue == NULL){
				CMS_LOG(LL_ERROR, "calloc parameter value fail.\n");
				return -1;
			}
			strcpy(paraList->parameter.parameterName, paraName);
			strcpy((char *)paraList->parameter.paraValue, value);
			paraList->parameter.typeSizeOfValue = typeOfValue;
			paraList->flag = 1;
		}
		else{
			newParaList = (objParaList_t *)calloc(1, sizeof(objParaList_t));
			if(newParaList == NULL){
				CMS_LOG(LL_ERROR,"malloc parameter list fail.\n");
				cms_freeObjectParaList(paraList);
				return -1;
			}

			if(typeOfValue == PARAMETER_TYPE_STRING){
				valueLen = strlen(value) + 1;
			}else{
				valueLen = (typeOfValue & ~((uint32_t)PARAMETER_TYPE_CLASS_BITS));
			}
			newParaList->parameter.paraValue = (uint8_t *)calloc(valueLen, sizeof(uint8_t));
			if(newParaList->parameter.paraValue == NULL){
				CMS_LOG(LL_ERROR, "calloc parameter value fail.\n");
				free(newParaList);
				cms_freeObjectParaList(paraList);
				return -1;
			}
			strcpy(newParaList->parameter.parameterName, paraName);
			strcpy((char *)newParaList->parameter.paraValue, value);
			newParaList->parameter.typeSizeOfValue = typeOfValue;
			newParaList->flag = 1;

			INSERT_NODE(paraList,newParaList);
		}
	}

	
	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_eventNotify(uint32_t eventId, uint32_t typeSizeOfContent, void *eventContent){
	CMS_LOG(LL_DEBUG, "Entrance");
	cmsMsgCb_t *pReqMsg = NULL;
	int ret = -1;

	pReqMsg = cms_createRequestMsg(0);
	if(pReqMsg == NULL){
		CMS_LOG(LL_ERROR,"notify event fail, create request msg fail.\n");
		return -1;
	}

	cms_requestEventNotify(pReqMsg, eventId, typeSizeOfContent, eventContent);
	ret = cms_sendMsg(pReqMsg,0);
	if(ret == -1){
		CMS_LOG(LL_ERROR, "send msg fail.\n");
		cms_msgAndShmFree(pReqMsg);
		return -1;
	}
	cms_msgFree(pReqMsg);

	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_objChgNotify(cmsMsgCb_t *objDataMsg){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pReqMsg = NULL;
	int ret = -1;

	pReqMsg = cms_createRequestMsg(0);
	if(pReqMsg == NULL){
		CMS_LOG(LL_ERROR,"notify obj change fail, create request msg fail.\n");
		return -1;
	}
	cms_requestObjChgNotify(pReqMsg, objDataMsg);
	ret = cms_sendMsg(pReqMsg,0);
	if(ret == -1){
		CMS_LOG(LL_ERROR, "recv msg fail.\n");
		cms_msgAndShmFree(pReqMsg);
		return CMS_NOK;
	}
	cms_msgFree(pReqMsg);
	
	CMS_LOG(LL_DEBUG, "Exit\n");
	return CMS_OK;
}

int cms_objParaSent(cmsMsgCb_t *objDataMsg, uint32_t dtsModuleId){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pReqMsg = NULL;
	int ret = -1;

	pReqMsg = cms_createRequestMsg(0);
	if(pReqMsg == NULL){
		CMS_LOG(LL_ERROR,"notify obj change fail, create request msg fail.\n");
		return -1;
	}
	cms_requestPutPara(pReqMsg, objDataMsg);
	ret = cms_sendMsg(pReqMsg,dtsModuleId);
	if(ret == -1){
		CMS_LOG(LL_ERROR, "recv msg fail.\n");
		cms_msgFree(pReqMsg);
		return -1;
	}
	cms_msgFree(pReqMsg);
	
	CMS_LOG(LL_DEBUG, "Exit\n");
	return 0;
}

int cms_eventRecv(cmsEvent_t **eventBuf)
{
	CMS_LOG(LL_DEBUG, "event recv");
	struct msqid_ds stat;
	void *queueBuf = NULL;
	int ret = CMS_NOK;
	
	/*malloc eventBuf, and recv message*/
	if(*eventBuf == NULL){
		if((queueBuf = (cmsEvent_t *)malloc(sizeof(cmsEvent_t))) == NULL){
			CMS_LOG(LL_ERROR, "eventbuf malloc fail.\n");
			goto error;
		}
		*eventBuf = queueBuf;
	}


	/*recv event*/
	ret = msgrcv(cmsEventId, (struct msgbuf *)(*eventBuf),sizeof(cmsEvent_t)-sizeof(long), 0, 0);
	if(ret == -1){
		if(errno == E2BIG){
			CMS_LOG(LL_DEBUG, "msg queue data size is greater than malloc size.\n");

			free(*eventBuf);
			*eventBuf = NULL;
			
			memset(&stat, 0,sizeof(struct msqid_ds));
			msgctl(cmsEventId, IPC_STAT, &stat);
			if((stat.msg_qnum == 0) || (stat.msg_cbytes == 0)){
				goto error;
			}
			if((*eventBuf = (cmsEvent_t *)malloc(sizeof(long) + stat.msg_cbytes)) == NULL){
				CMS_LOG(LL_ERROR, "eventbuf malloc fail.\n");
				goto error;
			}
			ret = msgrcv(cmsEventId, (struct msgbuf *)(*eventBuf),stat.msg_cbytes, 0, 0);
			if(ret == -1){
				CMS_LOG(LL_ERROR, "recv event fail, errno %d %s.\n", errno, strerror(errno));
				goto error;
			}
		}else{
			CMS_LOG(LL_DEBUG, "recv event fail, errno %d %s.\n", errno, strerror(errno));
			goto error;
		}
	}
	
	return CMS_OK;

error:
	return ret;
}

cmsMsg_header_t* cms_parseMsgHeader(const cmsMsgCb_t *pMsg){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsg_header_t *pMsgHeader = NULL;

	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "pMsg is NULL.\n");
		return NULL;
	}

	pMsgHeader = (cmsMsg_header_t *)((uint8_t *)(pMsg->shmAddr) + pMsg->msgOffset);
	CMS_LOG(LL_DEBUG, "Exit\n");
	return pMsgHeader;
}

cmsMsg_action_t *cms_parseMsgAction(cmsMsgCb_t *pMsg){
	CMS_LOG(LL_DEBUG, "Extrance\n");
	cmsMsgCb_t *pCurMsg = NULL;
	cmsMsg_action_t *curAction = NULL;
	actionType_t *lastAction = NULL;
	uint32_t actionType;

	/*parameter check*/
	if(pMsg == NULL){
		CMS_LOG(LL_ERROR, "pMsg is NULL.\n");
		return NULL;
	}

	/*get action pointer*/
	pCurMsg = (cmsMsgCb_t *)(pMsg->pCurMsgCb);
	pMsg->action.action = (actionType_t *)(pCurMsg->pCurData);
	curAction = &(pMsg->action); 

	/*start parse*/
	GET_TYPE(actionType, curAction->action->type);
	CMS_LOG(LL_DEBUG, "action type 0x%x, start parse action.\n",actionType);
	switch(actionType){
	case CMS_MSG_ACT_REGMODULE:
	case CMS_MSG_ACT_UNREGMODULE:
	case CMS_MSG_ACT_GETMODULEID:
		curAction->module = (cmsModule_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
		CMS_LOG(LL_DEBUG, "srcPid=%d modulename: %s, moduleMsqId: %d, eventMsqId: %d********************.\n",
			curAction->module->pid,
			curAction->module->moduleName,curAction->module->moduleMsqId, 
			curAction->module->eventMsqId);

		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)*3
			+ sizeof(uint8_t) + curAction->module->sizeofModuleName;

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		break;
	
	case CMS_MSG_ACT_SUBSCRIBEEVENT:
		curAction->event = (event_t *)((pCurMsg->pCurData) + sizeof(actionType_t));
	
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t)
			+ sizeof(uint32_t)*(2 + curAction->event->eventCount);

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		break;
	case CMS_MSG_ACT_UNSUBSCRIBEEVENT:
		curAction->event = (event_t *)((pCurMsg->pCurData) + sizeof(actionType_t));

		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t)
			+ sizeof(uint32_t)*(2 + curAction->event->eventCount);

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		break;
	case CMS_MSG_ACT_CMD_SERVICE:
		curAction->cmdService = (cmdService_t *)((pCurMsg->pCurData) + sizeof(actionType_t));

		/*move pointer to next action*/
		if(curAction->cmdService->typeSizeOfContent == PARAMETER_TYPE_STRING)
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t)*2
				+ strlen(curAction->cmdService->content) + 1;
		else
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t)*2
				+ (curAction->cmdService->typeSizeOfContent & ~((uint32_t)PARAMETER_TYPE_CLASS_BITS));

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		break;
	case CMS_MSG_ACT_SEL:
		curAction->object = (union object_u *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));

		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t) + sizeof(uint8_t)
			+ curAction->object->objName.sizeofName;

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		break;
	case CMS_MSG_ACT_OBJECT_RESTORE:
	case CMS_MSG_ACT_SELBYOFFSET:
	case CMS_MSG_ACT_SELID:
		curAction->object = (union object_u *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));

		/*move pointer to next action*/
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)*2
			+ sizeof(uint64_t);

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		break;
	case CMS_MSG_ACT_GETPARA:
		curAction->parameter = (parameter_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint8_t) + curAction->parameter->sizeofParameter;

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		
		curAction->paraValue = (value_t *)pCurMsg->pCurData;
		
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t)*3;
		
		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

		break;
	case CMS_MSG_ACT_SETPARA:
		curAction->parameter = (parameter_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint8_t) + curAction->parameter->sizeofParameter;
		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

		curAction->paraValue = (value_t *)pCurMsg->pCurData;
		/*move pointer to next action*/
		if(curAction->paraValue->typeSizeOfValue == PARAMETER_TYPE_STRING)
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t)
				+ strlen((char *)curAction->paraValue->value) + 1;
		else
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t)
				+ (curAction->paraValue->typeSizeOfValue & ~((cmsuint32)PARAMETER_TYPE_CLASS_BITS));

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		break;
	case CMS_MSG_ACT_DELOBJBYID:
	case CMS_MSG_ACT_ADDOBJBYID:
		curAction->object = (union object_u *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));

		/*move pointer to next action*/
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)*2
			+ sizeof(uint64_t);

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		
		break;
	case CMS_MSG_ACT_DELOBJBYPATH:
	case CMS_MSG_ACT_ADDOBJBYPATH:
		curAction->object = (union object_u *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));

		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t) + sizeof(uint8_t)
			+ curAction->object->objName.sizeofName;

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		
		break;
	case CMS_MSG_ACT_GETOBJ:
		curAction->object = (union object_u *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)*2 + sizeof(uint64_t);
		
		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		break;
	case CMS_MSG_ACT_GETATTR:
		curAction->paraAttr = (attr_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
		
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)*10
			+sizeof(uint8_t) + curAction->paraAttr->parameter.sizeofParameter;
		
		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		
		break;
	case CMS_MSG_ACT_SETATTR:
		curAction->paraAttr = (attr_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
		
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)*2
			+sizeof(uint8_t) + curAction->paraAttr->parameter.sizeofParameter;

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		
		break;
	case CMS_MSG_ACT_PARAMETERINFO:
		curAction->paraValue = (value_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));

		/*move pointer to next action*/
		if(curAction->paraValue->typeSizeOfValue == PARAMETER_TYPE_STRING)
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)
				+ strlen((char *)curAction->paraValue->value) + 1;
		else
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)
				+ (curAction->paraValue->typeSizeOfValue & ~((uint32_t)PARAMETER_TYPE_CLASS_BITS));
		
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		
		curAction->paraAttr = (attr_t *)pCurMsg->pCurData;

		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t)*2 + sizeof(uint8_t) + curAction->paraAttr->parameter.sizeofParameter;

		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		break;
	case CMS_MSG_ACT_GET_APP_EVENT:
	case CMS_MSG_ACT_PRE_DEFAULT:
	case CMS_MSG_ACT_RESTORECONF:
		curAction->file = (file_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
		
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)
			+ sizeof(uint8_t) + curAction->file->sizeOfFileName;
		
		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		
		break;
	case CMS_MSG_ACT_GET_ALL_EVENT:
	case CMS_MSG_ACT_QUEUE_CLEAN:
	case CMS_MSG_ACT_SET_LOG:
	case CMS_MSG_ACT_SETMODULEID:
	case CMS_MSG_ACT_RESTORDEFAULT:
		curAction->eventContent = (value_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
			
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(value_t);
		
		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		
		break;
	case CMS_MSG_ACT_SAVE:
		curAction->save = (save_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
		
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(save_t);
		
		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		
		break;
	case CMS_MSG_ACT_EVENT_NOTIFY:
		curAction->event = (event_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
		
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)*(2 + curAction->event->eventCount);

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

		curAction->eventContent = (value_t *)(pCurMsg->pCurData);

		/*move pointer to next action*/
		if(curAction->eventContent->typeSizeOfValue == PARAMETER_TYPE_STRING)
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t)
				+ strlen((char *)curAction->eventContent->value) + 1;
		else
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(uint32_t)
				+ (curAction->eventContent->typeSizeOfValue & ~((uint32_t)PARAMETER_TYPE_CLASS_BITS));

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);

		break;
	case CMS_MSG_ACT_PUT_PARAINFO:
	case CMS_MSG_ACT_OBJCHG_NOTIFY:
		CMS_LOG(LL_DEBUG, "action type 0x%x.\n",actionType);
		curAction->objDataShm = (objectNotify_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
		
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t);

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		break;
	case CMS_MSG_ACT_OBJCHG_DATA:
		CMS_LOG(LL_DEBUG, "action type 0x%x, 0x%x.\n",actionType, curAction->action->type);
		//CMS_LOG(LL_DEBUG, "parse obj change 00, , mem %d===============\n", (pCurMsg->pCurData - pCurMsg->shmAddr));
		curAction->objectData = (objectChgData_t *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));
		/*move pointer to next action*/
		pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)*3+sizeof(uint64_t);/*?? when sizeof objectChgData_t struct, size is different*/
		//CMS_LOG(LL_DEBUG, "parse obj change 0, objsize %d, mem %d, %d===============\n",  curAction->objectData->objectSize, (pCurMsg->pCurData - pCurMsg->shmAddr), sizeof(objectChgData_t));
		if((curAction->objectData->objectSize & CMS_DB_SET_OBJECT) == CMS_DB_SET_OBJECT){
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + (curAction->objectData->objectSize & CMS_DB_OBJ_SIZE_MASK)*2;
		}else{
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + (curAction->objectData->objectSize & CMS_DB_OBJ_SIZE_MASK);
		}

		//CMS_LOG(LL_DEBUG, "parse obj change 1, objsize %d, mem %d===============\n",  (curAction->objectData->objectSize &CMS_DB_OBJ_SIZE_MASK), (pCurMsg->pCurData - pCurMsg->shmAddr));
		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		//CMS_LOG(LL_DEBUG, "parse obj change 2, objsize %d, mem %d===============\n",  curAction->objectData->objectSize, (pCurMsg->pCurData - pCurMsg->shmAddr));
		break;
	case CMS_MSG_ACT_UPDATE:
		CMS_LOG(LL_DEBUG, "action type udpate, 0x%x.\n",actionType);
		curAction->object = (union object_u *)((uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t));

		/*move pointer to next action*/
			pCurMsg->pCurData = (uint8_t *)(pCurMsg->pCurData) + sizeof(actionType_t) + sizeof(uint32_t)*2
			+ sizeof(uint64_t);

		/*padding*/
		pCurMsg->pCurData = (void *)(((uintptr_t)(pCurMsg->pCurData) + 0x3) & ~0x3);
		break;
	default:
		break;
	}
	CMS_LOG(LL_DEBUG, "action type 0x%x, 0x%x, end parse action.\n",actionType, curAction->action->type);

	lastAction = (actionType_t *)(pCurMsg->pCurData);
	if((lastAction->type == 0) && (pCurMsg->next != NULL)){
		pMsg->pCurMsgCb = pCurMsg->next;
	}
	CMS_LOG(LL_DEBUG, "Exit\n");
	return curAction;
}

void cms_cleanZombieMsg(void)
{
	cmsStatsBuf_t statsBuf;
	long msgType = 0;
	int ret = -1;
		
	srand(gettid());
	msgType = rand();
	do{
		ret = msgrcv(cmsModuleId, &statsBuf, sizeof(cmsStatsBuf_t)-sizeof(long), msgType, IPC_NOWAIT);
	}while(ret != -1);
}

